2

我是 c# 的新手-我想在按下 enter 时触发按钮单击事件。我的代码无法正常工作 -

问题是,当我在提交某些值时按 Enter 键时,它会显示应该显示的消息框,但是在按 Enter 键以获得 Messagebox 的 OK 按钮时,即使我没有按 Enter 键或输入任何其他值,它也会再次自动触发按钮单击事件。

public partial class Form1 : Form
{
    int n1, n2 = 0;
    private static readonly Random getrandom = new Random();
    private static readonly object syncLock = new object();
    public int GetRandomNumber(int min, int max)
    {
        lock (syncLock)
        { // synchronize
            return getrandom.Next(min, max);
        }
    }

    public int tQuestion()
    {
        n1 = GetRandomNumber(2, 11);
        n2 = GetRandomNumber(2, 11);

        string tQues = n1 + " x " + n2 + " = ";

        label1.Text = tQues;

        return 0;
    }


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        tQuestion();

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.KeyDown += new KeyEventHandler(tb_KeyDown);
    }

    public void button1_Click(object sender, EventArgs e)
    {

        string tAns = textBox1.Text;
        int answer = n1 * n2;

        string tOrgAns = answer.ToString();

        if (tAns == tOrgAns)
            MessageBox.Show("Your answer is Corect", "Result", MessageBoxButtons.OK,MessageBoxIcon.Exclamation );
        else
            MessageBox.Show("Your answer is WRONG", "Result", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        textBox1.Text = "";
        textBox1.Focus();
        tQuestion();
    }

    static void tb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            button1_Click(sender, e);
        }
    }

此外,代码仅在我static从中删除时才有效static void tb_KeyDown(object sender, KeyEventArgs e)- 否则会出错:

非静态字段、方法或属性需要对象引用

请帮助我 - 我是 C# 和 .Net 的新手

4

8 回答 8

3

将按钮单击事件处理程序提取到单独的方法(例如VerifyAnswer)并从两个地方调用它:

public void button1_Click(object sender, EventArgs e)
{
    VerifyAnswer();
}

// NOTE: static modifier removed
private void tb_KeyDown(object sender, KeyEventArgs e) 
{
    if (e.KeyCode == Keys.Enter)        
        VerifyAnswer();
}

private void VerifyAnswer()
    string tAns = textBox1.Text;
    int answer = n1 * n2;

    string tOrgAns = answer.ToString();

    if (tAns == tOrgAns)
        MessageBox.Show("Your answer is Corect", "Result", MessageBoxButtons.OK,MessageBoxIcon.Exclamation );
    else
        MessageBox.Show("Your answer is WRONG", "Result", MessageBoxButtons.OK, MessageBoxIcon.Stop);
    textBox1.Text = "";
    textBox1.Focus();
    tQuestion();
}

不要试图手动执行事件处理程序——它们的目的只是处理事件。

于 2013-07-23T13:12:46.813 回答
2

您在文本框聚焦时调用。更好的选择是创建一个单独的函数并从两者中调用它。

我已对您的代码进行了更改。它对我有用。您需要将tb_keyDown事件与keyDown文本框属性中的属性链接。

试试这个代码:

   public partial class Form1 : Form
{
    int n1, n2 = 0;
    private static readonly Random getrandom = new Random();
    private static readonly object syncLock = new object();
    public int GetRandomNumber(int min, int max)
    {
        lock (syncLock)
        { // synchronize
            return getrandom.Next(min, max);
        }
    }

    public int tQuestion()
    {
        n1 = GetRandomNumber(2, 11);
        n2 = GetRandomNumber(2, 11);

        string tQues = n1 + " x " + n2 + " = ";

        label1.Text = tQues;

        return 0;
    }


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        tQuestion();

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //make it empty. You need to attach tb_KeyDown event in properties
    }

    public void button1_Click(object sender, EventArgs e)
    {

        CheckAnswer();
    }

    void tb_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            CheckAnswer();
        }
    }

    private void CheckAnswer()
    {
        string tAns = textBox1.Text;
        int answer = n1 * n2;

        string tOrgAns = answer.ToString();

        if (tAns == tOrgAns)
            MessageBox.Show("Your answer is Corect", "Result", MessageBoxButtons.OK,MessageBoxIcon.Exclamation );
        else
            MessageBox.Show("Your answer is WRONG", "Result", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        textBox1.Text = "";
        textBox1.Focus();
        tQuestion();
 }   



}
于 2013-07-23T13:20:14.737 回答
1

您可能希望使用静态无参数 keylistener,您可以在静态方法中单独运行它来检查键输入。然后在那里解析关键输入

于 2013-07-23T13:13:05.493 回答
1

那是因为这段代码:

static void tb_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        button1_Click(sender, e);
    }
}

删除该处理程序并将 的设置AcceptButton为。这是您可以在设计器中简单设置的属性。Formbutton1AcceptButton

于 2013-07-23T13:13:24.633 回答
0

请检查以下选项:

(1)Forms Properties 窗口不能使用 AcceptButton 吗?这设置了“Enter”键按下的默认行为,但您仍然可以使用其他快捷键。(2)

static void tb_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == (char)13)
    {
        button1_Click(sender, e);
    }
}
于 2013-07-23T13:15:44.057 回答
0

您不能从静态成员访问实例成员

要么你必须

  • 使方法成为实例方法(去掉 static 关键字)
  • 使字段/方法成为静态(添加静态关键字)

您选择哪个取决于该字段是否应在所有实例之间共享。

希望它会有所帮助

于 2013-07-23T13:13:00.130 回答
0

你在找那个吗

button1.PerformClick()
于 2013-07-23T13:13:54.090 回答
0

不要试图触发按钮点击。本质上,您应该做的是重构代码,以便将事件处理程序与实际实现分开。例如

ButtonClick(...)
{
   ExecuteMethod();
}

KeyDown(...)
{
   ExecuteMethod();
}

ExecuteMethod()
{
   // Actual implementation.
}

这将取消您订阅的事件与实现的关联。这样,将来,如果您希望添加新按钮或更改事件处理程序,您的实际逻辑实现将保持不变。

于 2013-07-23T13:14:09.927 回答