Handling Messages


For a program to respond to a button press (or any other type of control interaction), it must handle the message that the button generates. In general, when a user interacts with a control, those interactions are passed to your program as messages. In a forms-based C# program, these messages are processed by event handlers. Therefore, to receive messages, your program adds its own event handler onto the list of handlers called when a message is generated. For button-press messages, this means adding your handler to the Click event.

The Click event is defined by Button. (Click is inherited from Control.) It has this general form:

 public Event EventHandler Click;

The EventHandler delegate is defined as shown here:

 public delegate void EventHandler(object who, EventArgs args)

The object that generated the event is passed in who. Any information associated with that event is passed in args. For many events, args will be an object of a class derived from EventArgs. Since a button click does not require any additional information, we don’t need to worry about event arguments when handling the button.

The following program adds button-response code to the preceding program. Each time the button is clicked, the location of the button is changed.

 // Handle button messages. using System; using System.Windows.Forms; using System.Drawing; class ButtonForm : Form {   Button MyButton = new Button();   public ButtonForm() {     Text = "Respond to a Button";     MyButton = new Button();     MyButton.Text = "Press Here";     MyButton.Location = new Point(100, 200);     // Add button event handler to list.     MyButton.Click += MyButtonClick;     Controls.Add(MyButton);   }   [STAThread]   public static void Main() {     ButtonForm skel = new ButtonForm();     Application.Run(skel);   }   // Handler for MyButton.   protected void MyButtonClick(object who, EventArgs e) {     if(MyButton.Top == 200)       MyButton.Location = new Point(10, 10);     else       MyButton.Location = new Point(100, 200);   } }

Let’s look closely at the event handling code in this program. The event handler for the button click is shown here:

 // Handler for MyButton. protected void MyButtonClick(object who, EventArgs e) {   if(MyButton.Top == 200)     MyButton.Location = new Point(50, 50);   else     MyButton.Location = new Point(100, 200); }

MyButtonClick( ) is compatible with the EventHandler delegate shown earlier, which means that it can be added to the Click event chain. Notice that it is modified with protected. This is not technically necessary, but it is a good idea because event handlers are not intended to be called except in response to events.

Inside the handler, the location of the top of the button is determined from the Top property. All controls define the following properties, which specify the coordinates of the upper-left and lower-right corners:

 public int Top { get; set; } public int Bottom { get; } public int Left { get; set; } public int Right { get; }

Notice that the location of the control can be changed by setting Top and Left, but not by setting Bottom and Right, because they are read-only. (To change the size of a control, you can use the Width and Height properties.)

When the button click event is received, if the top of the control is at its original location of 200, the location is changed to 10, 10. Otherwise, it is returned to its original location of 100, 200. Therefore, each time you click the button, the location of the button changes.

Before MyButtonClick( ) can receive messages, it must be added to the event handler chain linked to the button’s Click event. This is done inside the ButtonForm constructor, using this statement:

 MyButton.Click += MyButtonClick;

Notice that this statement utilizes the new method group conversion syntax for assigning a method to a delegate. After the method is added to the Click event, each time the button is clicked, MyButtonClick( ) is called.

An Alternative Implementation

As a point of interest, MyButtonClick( ) could have been written in a slightly different way. Recall that the who parameter of an event handler receives a reference to the object that generated the call. In the case of a button click event, this is the button that was clicked. Thus, MyButtonClick( ) could have been written like this:

 // An Alternative button handler. protected void MyButtonClick(object who, EventArgs e) {   Button b = (Button) who;   if(b.Top == 200)     b.Location = new Point(10, 10);   else     b.Location = new Point(100, 200); }

In this version, who is cast to Button, and this reference (rather than the MyButton field) is used to access the button object. Although there is no advantage to this approach in this case, it is easy to imagine situations in which it would be quite valuable. For example, such an approach allows a button event handler to be written independently of any specific button.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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