Every morning at the crack of dawn Skyhawk133 gets up to the sounds of the rooster crowing and puts on a pot of coffee. He has just enough time to take a shower and grab a quick bite before he is off to the Dream.In.Code (DIC) Power company to start the day of powering the worlds programming ideologists. The work day starts as normal where the DIC electricians and programming engineers come in and begin their day. Of course I wander in half drunk and ready to play with some high voltage. Skyhawk assigns me my first duty of the morning, some good old wiring. So grab that toolbelt, hop in the bucket on the back of the white truck and lets go out to parts unknown! We will wire up some controls dynamically in .NET on this entry of the Programming Underground!
While the term “wiring up a control” is not necessarily an industry wide term, it is used often enough to explain the process. For demonstration purposes I am going to be using VB.NET but you could certainly use VC++ or C# to do the same thing. Wiring up a control is the process of dynamically assigning a function to handle an event (like click or keypressed) for a control (like a button or a combobox). This is good for many reasons. First it allows us to create buttons dynamically and then attach events to them after they have been created in code. We don’t need to create the control first and go through the IDE to attach it to an event. Heck, we may be creating dozens of buttons and not exactly know how many buttons until runtime. So how could we attach them to an event? Secondly, it can allow us to share one function with many controls without having to specify a huge list of delegates in the functions signature.
I got the idea for this blog entry from a question on the board recently where someone wanted to first know how to create a control through code and then was looking to attach an event to it. As part of my answer (like many of my answers) I gave a quick example which I will show here now.
Public Class Form1 ' Our button event that kicks off the whole process. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Create the button dynamically and set its properties. Dim mybutton As New Button mybutton.Left = 10 mybutton.Top = 10 mybutton.Text = "My New Button" ' Use the addhandler and pass it first the event we want it to handle (here we use the click event) ' Then we give it the address of our custom function called PrintMessage. ' AddressOf is used to get the address of our function... think of it as pointing to the function. AddHandler mybutton.Click, AddressOf PrintMessage ' Now we add the button to the form. Me.Controls.Add(mybutton) End Sub ' Our custom function. Notice how it has the same parameters as a standard click event signature. ' We must have these parameters setup like this so our custom function knows the sender and any argumeents passed. Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show("Dynamic event happened!") End Sub End Class
This program uses a button to add another button to the form and attach an event which will print out “Dynamic event happened!”. It is rather simple and only requires that you put a button on your form called “Button1”.
When we click the button, the above code will fire and we will go through several steps. The first step is creating the button called “mybutton”. We declare a variable of type “Button” and use the New keyword so that it will create an instance. The second step is that we set this new button’s properties including its location to be in the upper left corner of our form at coordinates 10, 10. Next we give it some text to show on the button. Here I used the text “My New Button”.
Now here is the magical line we use to do the wiring. We use a function called AddHandler which takes two parameters. The first parameter is the object with its event that we want to handle. In this case we specify our new button and that we want to handle the click event. The second parameter uses AddressOf to get a pointer to a function we have created. In this case we created a function called “PrintMessage”. Notice how our PrintMessage function has the same parameters as your standard click event for a button. The first parameter being the sender (in this case our button mybutton) and the second parameter is the event arguments. You must specify your function this way so that it knows who is sending the event and can receive any arguments passed along with it.
Lastly, after you have wired up the control, we simply add the control to the form’s controls collection. The result is a form where if you press the button it will create another button that reads “My New Button” and when you click it, it will show “Dynamic event happened!” in a messagebox.
Wiring controls are good for creating multiple unknown controls and then having them all wired up to individual functions or all controls wired to the same function dynamically. With a simple loop a programmer can create 10 buttons, wire them all up to the same PrintMessage function using AddHandler and use PrintMessage to determine which button was pressed (which is why you needed the sender parameter on your function).
As stated before this is something in common to the .NET Framework and will work in a similar fashion on VC++ as well as C#. If you are finding your delegate lists getting long for a function, don’t know how many buttons you will need, or want to share a function amongst several controls, wiring up is where it is at.
Well… it looks like the programming world is wired up, everyone has their power for the day and luckily you weren’t killed (yeah I forgot to mention wearing rubber gloves but that is no biggie). So we can have a few brews, maybe ride around on the bucket arm a little and hopefully skyhawk won’t find out. But if he asks about the job, we were working the entire time, had tons of down contorls, and I got in a fight with a bear (hopefully he doesn’t ask why). Now put on that hard hat before someone sees you.
Thanks for reading! 🙂