【问题标题】:C# Call a function when a second key is pressedC# 在按下第二个键时调用函数
【发布时间】:2017-10-17 00:28:36
【问题描述】:

当按下第二个键时,我失去了调用函数。我为我的按钮使用了 KeyDown 事件。该 KeyDown 将调用一个检查该按钮的函数。我的问题是检查该按钮后,用户必须按另一个 Enter 键或空格键才能继续下一个数据。

这是我的 radiobutton1 KeyDown 事件

private void btn1_KeyDown(object sender, KeyEventArgs e)
    {
        btn1.BackColor = Color.Blue;
        checkAns(btn1.Text, btn1);
    }

这是我的 checkAns 函数,它将检查选定的按钮

private void checkAns (string ansText, RadioButton rdo)
    {
        var row = dTable.Rows[currentRow];
        var ans = row["ANSWER"].ToString();
        if (ansText == ans)
        {
            rdo.BackColor = Color.Green;
            correctAdd();
            //MessageBox.Show("Correct");
        }
        else
        {
            rdo.BackColor = Color.Red;
            wrongAdd();
            //MessageBox.Show("Wrong. Answer is" + " \n " + ans);
        }
        nextEnter (------); //Here I'm not sure how to call the another keydown/keypress event or value of the enter key
    }

这是我的 nextEnter 函数

private void nextEnter(------) //Also at this part.
    {
        if (------ == Keys.Enter) //And here.
        currentRow++;
        currentNo++;
        remain--;
        nextRow();
    }

【问题讨论】:

  • 您可以将KeyEventArgsKeyCode 属性从btn1_KeyDown 传递给checkAns,然后再传递给nextEnter
  • @MetaColon,对不起,我不明白。请纠正我。关键代码(输入),即对象?去检查员?

标签: c# visual-studio function event-handling


【解决方案1】:

我通过在 enter keydown 事件期间让表单增加一个变量来解决这个问题。

private void frmTest_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Space)
        {
            entCount++;
        }
    }

在entCount == 2时使用if语句,显示下一个数据并将entCount重置为0。

【讨论】:

    【解决方案2】:

    为了证明我在评论中的意思:

    您可以从btn1_KeyDown 传递KeyEventArgsKeyCode 属性

    private void btn1_KeyDown (object sender, KeyEventArgs e)
    {
        btn1.BackColor = Color.Blue;
        checkAns (btn1.Text, btn1, e.KeyCode);
    }
    

    checkAns

    private void checkAns (string ansText, RadioButton rdo, Keys pressedKey)
    {
        var row = dTable.Rows [currentRow];
        var ans = row ["ANSWER"].ToString ();
        if (ansText == ans)
        {
            rdo.BackColor = Color.Green;
            correctAdd ();
            //MessageBox.Show("Correct");
        }
        else
        {
            rdo.BackColor = Color.Red;
            wrongAdd ();
            //MessageBox.Show("Wrong. Answer is" + " \n " + ans);
        }
        nextEnter (pressedKey); //Here I'm not sure how to call the another keydown/keypress event or value of the enter key
    }
    

    然后继续nextEnter

    private void nextEnter (Keys key) //Also at this part.
    {
        if (key == Keys.Enter) //And here.
        currentRow++;
        currentNo++;
        remain--;
        nextRow ();
    }
    

    如果我误解了什么,请告诉我,您需要进一步的帮助,否则我的解决方案对您不起作用。

    【讨论】:

    • 行为仍然相同。按回车键一次。将转到下一个数据。
    • 啊,现在我明白了你的问题——在这种行为中,你确实必须用计数器来解决它。
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2021-02-19
    相关资源
    最近更新 更多