Termination Buttons

Since many dialog boxes are terminated by clicking on a button, the .NET Framework makes this process even easier than shown in the previous examples.

The Button control implements the IButtonControl interface, which provides a DialogResult property. You can assign any valid DialogResult value to any button. The value of this DialogResult property must be one of the members of the same DialogResult enumeration used by the Form.DialogResult property, shown in Table 6-4. When a button in a modal dialog box is clicked, its DialogResult property is assigned to the dialog box. This has the effect of terminating the dialog box with that DialogResult value.

To demonstrate this idea, comment out the Click event handlers for the btnOK and btnCancel buttons in the ongoing example (dlg.cs for the C# version, dlg.vb for the VB.NET version).

Warning

In the C# version, also remember to expand the code in the Windows Form Designer generated code region, find the lines that add the event-handler methods to the Click delegate for each button, and delete them. These lines will look like:

image with no caption

this.btnOK.Click += new 
      System.EventHandler(this.btnOK_Click);
this.btnCancel.Click += new 
      System.EventHandler(this.btnCancel_Click);

If you run the application now, the only way to close the dialog box is by clicking on the Close button in the upper-right corner, clicking on Close in the system menu, ...

Get Programming .NET Windows Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.