使Enter键像提交按钮一样

时间:2010-05-12 15:24:20

标签: c#

如果某个人按下我表单上的回车键,我该如何设置它会提交此代码?

我尝试了KeyPress活动等......但我似乎无法弄明白。

 private void
 xTripSubmitButton_Click(object sender, EventArgs e) { 
    //  Calculates the numbers from the input and output boxes/labels
    Miles = double.Parse(this.xTripDestinationTextBox.Text);
    Mpg = double.Parse(this.xTripMpgTextBox.Text);
    Price = double.Parse(this.xTripPricepgTextBox.Text);
    Output = Miles / Mpg;
    Coutput = Output * Price;

    //displays a message and the result for the numbers the user inputs
    this.xTripOutputLabel.Text = "Total gallons you would use: " +
        Output.ToString("0") +
        Environment.NewLine + "Total amount it will cost you: " +
         Coutput.ToString("C"); 

    }

4 个答案:

答案 0 :(得分:6)

您可以将表单的 AcceptButton 属性设置为您的按钮:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.acceptbutton.aspx

  

“获取或设置用户按下ENTER键时单击的表单上的按钮。”

答案 1 :(得分:4)

如果这是WinForms,请将表单的AcceptButton属性设置为您想要点击模拟的按钮,当他们按 enter 时。然后,在该按钮的事件处理程序中,再调用Close()或更好,设置DialogResult属性

答案 2 :(得分:0)

如果你使用winforms我认为你做的是一个按键事件:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

答案 3 :(得分:0)

Enter是按键。您需要创建一个按键事件处理程序。