-
Notifications
You must be signed in to change notification settings - Fork 32
WinForms
Felix S. Klock II edited this page Jul 28, 2013
·
1 revision
Felix is using this page to take notes on programming a GUI via Windows Forms (.NET assembly System.Windows.Forms).
Many tutorials say you should subclass the Form
class and then use the following pattern:
Form form = new MyFirstForm();
Application.Run(form);
But an example that deviated from this pattern, that has formed the basis of all the event handling demos that Chris Burns wrote, is taken from the Microsoft SDK documentation for System.Windows.Forms.Form
, where it constructs an instance of the Form
class (not a subclass), and then calls the .ShowDialog()
method on the form object.
// Create a new instance of the form.
Form form1 = new Form();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new Button ();
Button button2 = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
...
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
Is this sufficient for our needs? For now I'll assume that it will do, simply because this seems to keep us in a uni-task model for a bit longer. . .