In windows forms application development, you can use a single method to handle events from multiple controls. Here is a simple definition of events from MSDN.
An event is an action which you can respond to, or “handle,” in code. Events can be generated by a user action, such as clicking the mouse or pressing a key; by program code; or by the system.
Let’s say you need to change the backcolor of a textbox or a listbox to light blue when the control gets the focus or when a user selects it. To do that you would normally write code to change the backcolor when the Control.Enter event fires as shown in this code below.
Private Sub TextBox1_Enter(ByVal sender As _ System.Object, ByVal e As_ System.EventArgs) Handles TextBox1.Enter TextBox1.BackColor = Color.LightBlue End Sub
And of course you will also need to write code for the Control.Leave event as shown below to restore the default background color when the attention leaves the control (this is because if you do not create the event handler for the leave event, the control will remain with the same background color even after the user has exited.
Private Sub TextBox1_Leave(ByVal sender As _ System.Object, ByVal e As _ System.EventArgs) Handles TextBox1.Leave TextBox1.BackColor = SystemColors.Window End Sub
So now imagine you have a form with 50 controls. How much code are you going to write? That’s 100 event handlers to perform almost the same task. A lot of unnecessary code right? Well fortunately enough there is an easy way to help you from writing all those unnecessary, repeating lines of code.
1. Open your IDE and create a windows forms application. Give it the name of your choice.
2. Add four textbox controls and change their names to txtFirst, txtLast, txtGender and txtBirth
3. Right click on an empty space on the form and select View Code.
4. Inside the class, Type the following piece of code.
There is our event handler, I’ll explain a little. What we did here is create a control, make sure we cast (or change the type of) the object (sender) that fired the event to a control, and then add some code to manipulate the control. In this case, change the backcolor to light blue. Private Sub Control_Enter(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
Dim ctr As Control = CType(sender, Control)
ctr.BackColor = Color.LightBlue
End Sub
This means that the Button1_Click method handles the Button1.Click event. The event itself is at the end of the line. You can include multiple events here, from multiple controls; this is to say you can use the same event handler to handle multiple events from different controls.
Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
'add some code here
End Sub
Private Sub Control_Enter(ByVal sender As _
System.Object, ByVal e As _ System.EventArgs) _
Handles txtFirst.Enter, txtLast.Enter, _
txtGender.Enter, txtBirth.Enter
Dim ctr As Control = CType(sender, Control)
ctr.BackColor = Color.LightBlue
End Sub
What we have done here is use the same event handler to handle the .Enter event from multiple controls, and we are changing the back color to light blue.
Now go back to the form designer, select each control and under the properties panel, click the lightning bolt button to display the events for the control. Click the arrow next to the Enter event, and select Control_Enter.
Run the form and click on the controls separately to see that the back color changes when you enter the control.
Now we will create another event handler to handle the leave event, so we can restore the control’s backcolor to its default color when the attention moves to a different control. Go to the form’s code, and paste the following code within the class to create the contor_leave event handler.
Private Sub Control_Leave(ByVal sender As _ System.Object,ByVal e As System.EventArgs) _ Handles txtFirst.Leave, txtLast.Leave, txtGender.Leave, txtBirth.Leave Dim ctr As Control = CType(sender, Control) ctr.BackColor = Color.White End Sub
Again as you did for the Enter event, go to the forms designer, select the properties for each controls, go to the events and under the Leave event. Click the arrow to display available event handlers, select Control_Leave and run the form to see that the backcolor is reset to the default white when you leave the control.
As you can see, it is as simple as that. You can use this technique to perform various tasks using single event handlers, to take care of events firing from multiple controls and of course you can do this according to your different specific needs. Again this post is just a scratch at the top, and your comments are welcome.



0 Responses to “Using a Single EventHandler for events from multiple controls.”