按Enter键调用按钮单击事件 - c#

时间:2013-07-23 13:09:18

标签: c# .net winforms

我是c#的新手 - 我想按Enter键触发按钮点击事件。我的代码无法正常工作 -

问题是,当我按Enter键提交一些值时,它会显示它应该显示的消息框,但是按下Messagebox的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#&达网络

8 个答案:

答案 0 :(得分: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();
}

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

我对您的代码进行了更改。它对我有用。您需要在文本框属性中将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();
 }   



}

答案 3 :(得分:0)

您无法从静态成员访问实例成员

要么你必须

  • 使方法成为实例方法(删除静态关键字)
  • 使字段/方法成为静态(添加静态关键字)

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

希望它会有所帮助

答案 4 :(得分:0)

这是因为这段代码:

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

删除该处理程序并将AcceptButton的{​​{1}}设置为Formbutton1是您可以在设计器中设置的属性。

答案 5 :(得分:0)

你在找那个

吗?
button1.PerformClick()

答案 6 :(得分:0)

请勿尝试触发按钮单击。基本上你应该做的是重构你的代码,以便事件处理程序与实际的实现分开。 e.g。

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

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

ExecuteMethod()
{
   // Actual implementation.
}

这解除了您从实施中订阅的事件。这样,将来,如果您希望添加新按钮或更改事件处理程序,您的实际逻辑实现将保持不变。

答案 7 :(得分:0)

请检查以下选项:

(1)您是否可以在Forms Properties窗口中使用AcceptButton?这会设置按“Enter”键的默认行为,但您仍然可以使用其他快捷方式。 (2)

static void tb_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == (char)13)
    {
        button1_Click(sender, e);
    }
}
相关问题