Michael Quiles
Michael Quiles

Reputation: 1151

Make the Enter key act like the submit button

How can I make it where some one presses the enter key on my form it will submit this code ?

Ive tried the KeyPress events etc... but I can't seem to figure it out.

 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"); 

    }

Upvotes: 1

Views: 4790

Answers (4)

Rowland Shaw
Rowland Shaw

Reputation: 38130

If this is WinForms, set the form's AcceptButton property to be the button you want to have a click simulated on, when they press enter. Then, in that button's event handler, call Close() or better still, set the DialogResult property

Upvotes: 4

cdmckay
cdmckay

Reputation: 32240

Enter is a key press. You need to make a key press event handler.

Upvotes: 0

Olivier Payen
Olivier Payen

Reputation: 15268

You can set the AcceptButton property of the form to your button :

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

"Gets or sets the button on the form that is clicked when the user presses the ENTER key."

Upvotes: 6

MUG4N
MUG4N

Reputation: 19717

Well if you use winforms what I think you do there is a keypress event:

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

Upvotes: 0

Related Questions