按下Enter键时通话按钮点击事件-C# - c#

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

问题是,当我按Enter提交某些值时,它会显示应显示的消息框,但在按Enter键以确认消息框的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的新手

参考方案

当文本框处于焦点状态时,您正在呼叫。更好的选择是创建一个单独的函数,然后从两者中调用该函数。

我对您的代码进行了更改。它为我工作。您需要将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();
 }   



}

无法将Summernote值存储到数据库中,它返回[object Object] - php

我试图用ajax / jquery将summernote值存储到数据库中,但是它只是将此值发送到数据库字段-----> [object Object]。HTML:<div class="form-group"> <label for="description">Desciption<…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…