Default Buttons on a Form
Windows forms programming in .NET has come a long way, and while many things are very similar, many have changed. One question I see many newbies asking about is how to simulate the default Enter button on a Windows Form. In .NET, it couldn't be easier.
The first thing you'll need to know is the difference in the way Forms are shown in .NET. In classic VB, if you had a form called Form2, you'd simply call Form2.Show. This was a weird approach for many object oriented programmers because they never declared or instantiated an instance of the form. How then, did a form come to exist? Well, it just did. In .NET, that's much different. You first need to declare an instance of the form, then instantiate it. Afterward you show it by either calling the Form's .ShowDialog method, or its Show Method. The difference is that if you call ShowDialog, the form is displayed Modally whereas show simply displays the form in a non-modal fashion.
Since ShowDialog is a method, it has a return type and the return type is DialogResult enumeration.
So, if I wanted to show a form Modally and respond to what the user did, something like the following code would work:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm2 = new Form2();
switch(frm2.ShowDialog())
{
case DialogResult.OK:
MessageBox.Show("OK");
break;
case DialogResult.Cancel:
MessageBox.Show("Cancel");
break;
}
} |
Once the call to ShowDialog was executed, you'd see something like the following:

However, if you simply hit the 'Ok' button, nothing would happen. In fact, the form would still be visible and execution would take place at the end of the switch statement. Why? Well, without any further information .NET doesn't know which of the two buttons (or however many you have) is the Accept button, which is the 'Cancel', which is the 'No' etc. If you wanted to get this to work, you have two basic choices. The first is that you could add some code behind each of the buttons:
private void btnOk_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
|
This would do two things for you: 1) It would return the correct DialogResult (assuming you put the code in the right place) 2) Close the dialog form. While this approach works, it forces you to explicitly state your intentions and forces you to write some extra code. In some instances this is desirable, in others it'd be overkill.
The easier way would be to just go into the form's properties box, and set its AcceptButton and CancelButton Properties. In doing so, when someone hits the Enter button, DialogResult.Ok is returned, if someone hits Escape, DialogResult.Cancel is returned. Remember that each form has a DialogResult Property which is referenced by me.DialogResult in VB.NET, this.DialogResult in C#. So, all you need to do is set these AcceptButton and CancelButton properties, and the form will take care of this for you:


After that, you can just hit Enter, Cancel etc and everything will happen automatically for you. There are a few other members to the DialogResult Enumeration and in many instances, you'll need to set these manually to take full advantage of them, but there are a lot of times you need simple Ok/Cancel Functionality, and nothing could be eaiser.