点击ENTER时点击触发按钮?

时间:2013-11-12 20:29:47

标签: c# forms button textbox

我已成功分配ENTER键以触发button1单击事件。当我按下ENTER时,它应该打开“Startmenu”形式。然而它确实这样做了两次。不仅仅是一次。我在代码中找不到问题,有人可以帮助我吗?

这是我的代码:

public Login()
{
    InitializeComponent();            
    this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
}

private void button1_Click_1(object sender, EventArgs e)
{

    if (somethingsomething == true)
    {
        Startmenu sm = new Startmenu();
        sm.Show();
    }

    else
    {
        MessageBox.Show("something");
    }    

}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        button1.PerformClick();
        e.Handled = true;
    }
}

2 个答案:

答案 0 :(得分:0)

在button1_Click_1中放置一个断点并检查callstack以查看 谁在触发电话。

答案 1 :(得分:0)

试试这个:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        button1_Click_1.Click(sender,e);
    }
}