Publishing Events in Windows Forms


Publishing Events in Windows Forms

If you are familiar with Microsoft Visual Basic, Microsoft Foundation Classes (MFC), or any of the other tools available for building GUI applications for Windows, you are aware that Windows uses an event-driven model to determine when to execute code. In Chapter 16, “Delegates and Events,” you saw how to publish your own events and subscribe to them. Windows Forms and controls have their own predefined events that you can subscribe to, and these events should be sufficient to handle most eventualities.

Processing Events in Windows Forms

Your task as a developer is to capture the events that you feel are relevant to your application and write the code that responds to these events. A familiar example is the Button control, which raises a “Somebody clicked the button” event when a user clicks it with the mouse or presses Enter when the button has the focus. If you want the button to do something, you write code that responds to this event. This is what you will do in the final exercise in this chapter.

Handle the Click event for the Clear button

  1. In Design View (on the View menu, click Designer), select the Clear button on MemberForm.

    When the user clicks the Clear button, you want the form to be reset to its default values.

  2. In the Properties window, click the Events button.

    graphic

    The list of properties is replaced with a list of events that you can intercept.

  3. Select the Click event.

  4. Type clearClick in the text box and press Enter.

    A new event method called clearClick is created and displayed in the Code And Text Editor window. Notice that the event method conforms to the convention in that it takes two parameters: the sender (an object) and additional arguments (an EventArgs). The Windows Forms runtime will populate these parameters with information about the source of the event and any additional information that might be useful when handling the event. You will not use these parameters in this exercise.

  5. In the body of the clearClick method, call the Reset method.

    The body of the method now should look exactly like this:

    private void clearClick(object sender, System.EventArgs e) {     this.Reset(); }

Handle the Click event for the Add button

The users will click the Add button when they have filled in all the data for a member and want to store the information. The Click event should validate the information entered to ensure it makes sense (for example, should you allow a tower captain to have less than one year of experience?) and, if it is okay, arrange for the data to be sent to a database or other persistent store. You will learn more about validation and storing data in later chapters. For now, the code for the Click event of the Add button will display a message box echoing the data input.

  1. Return to Design View and select the Add button.

  2. In the Properties window, ensure that you are displaying events rather than properties, type addClick in the Click event, and then press Enter.

    Another event method called addClick is created.

  3. Add the following code to the addClick method:

    string details; details = "Member name " + firstName.Text + " "      + lastName.Text + " from the tower at " + towerNames.Text; MessageBox.Show(details, "Member Information");

This block of code creates a string variable called details that it fills with the name of the member and the tower that the member belongs to. Notice how the code accesses the Text property of the TextBox and ComboBox to read the current values of those controls. The MessageBox class provides static methods for displaying dialog boxes on the screen. The Show method used here will display the contents of the details string in the body of the message box and will put the text “Member Information” in the title bar. Show is an overloaded method, and there are other variants that allow you to specify icons and buttons to display in the message box.

Handle the Closing event for the form

As an example of an event that can take a different set of parameters, you will also trap the FormClosing event for a form. The FormClosing event is raised when the user attempts to close the form but before the form actually closes. You can use this event to prompt the user to save any unsaved data or even ask the user whether she really wants to close the form—you can cancel the event in the event handler and prevent the form from closing.

  1. Return to Design View and select the form (click anywhere on the background of the form rather than selecting a control).

  2. In the Properties window, ensure that you are displaying events, type memberFormClosing in the FormClosing event, and then press Enter.

    An event method called memberFormClosing is created.

    You should observe that the second parameter for this method has the type FormClosingEventArgs. The FormClosingEventArgs class has a Boolean property called Cancel. If you set Cancel to true in the event handler, the form will not close. If you set Cancel to false (the default value), the form will close when the event handler finishes.

  3. Add the following statements to the memberFormClosing method:

    DialogResult key = MessageBox.Show(     "Are you sure you want to quit",     "Confirm",      MessageBoxButtons.YesNo,     MessageBoxIcon.Question); e.Cancel = (key == DialogResult.No);

    These statements display a message box asking the user to confirm whether to quit the application. The message box will contain Yes and No buttons and a question mark icon. When the user clicks either the Yes or No button, the message box will close and the button clicked will be returned as the value of the method (as a DialogResult—an enumeration identifying which button was clicked). If the user clicks No, the second statement will set the Cancel property of the CancelEventArgs parameter (e) to true, preventing the form from closing.

Delegates for Windows Forms Events

When you use the Properties window to define an event method (see Chapter 16), Visual Studio 2005 generates code that creates a delegate that references the method and subscribes to the event. If you look at the block of code that defines the Clear button in the InitializeComponent method in the MemberForm.Designer.cs file, you will see the following statement:

//  // clear //  ... this.clear.Click += new System.EventHandler(this.clearClick);

The statement creates an EventHandler delegate pointing to the clearClick method. It then adds the delegate to the Click event for the Clear button. As you create additional event methods, Visual Studio 2005 will generate the required delegates and subscribe to the events for you.




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net