--------------------------
Before deletion of Button1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Hello World1")
End Sub
you will see hello world 1
--------------------------
After deletion of Button1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Hello World1")
End Sub
after deletion if you notice signature goes off ( Handles Button1.Click) delegate event handler is lost.
--------------------------
adding new button1
Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Hello World2")
End Sub
--------------------------
Now new delegate event handler is created ( Handles Button1.Click) is created for new control.
So your new control will point to new event created above.
If you want your new button to point your old code, you new to add delegate signature at the end of the old function as shown above
hope it helps.
--------------------------
you will now see Hello World 2
--------------------------
Main Topics
Browse All Topics





by: toddhdPosted on 2005-07-20 at 06:32:11ID: 14484333
There are two ways for a control to reference a given event function. One is by the name of the function, the other is by the 'Handles' keyword. The 'Handles' keyword is the real key here, but let's look at both. Consider the following:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Notice that the name of the function (Form1_Load) states both the name of the object, and the event that is happening. This makes is it clear what the function is handling. Sometimes, when you add a new control, it does not add the 'Handles' keyword at the end, so the program will simply assume that the function that names the object and event in this manner is the function that needs to be called.
The 'Handles MyBase.Load' part however, makes it explicitly clear that this function handles the Load event of the control named MyBase. In fact, we could do this:
Private Sub TheDingoAteYourBaby(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
This same function would STILL handle the Form1_Load event, because we used the Handles keyword to define it as such. (You can in fact, specify that this function handles more than one control and event)
Anyway, to answer your question - the first time you add a new control, and double click it to add a Click() event, VS.NET typically DOES NOT add the Handles keyword. However, if you delete it and re-add it, even naming it the same, the VS.NET tyically DOES add the handles keyword, hence, you get two codebases.
I find it best, rather than double clicking the control, to select it from the code behind dropdowns, and select the function, thus adding the Handles part, or just adding in Handles by yourself.