Using Handles in VB.Net

I am trying to understand an example on a book :
there is a form an it has 3 textboxes :txtbox1,txtbox2,txtbox3
and 2 command buttons. what I don't understand is there was no control with Control name , but this Sub procedure below shows  Control_TextChanged, I am confused.

If someone has read Murach's Beginning Visual Basic.Net can find this example.

Private Sub Control_TextChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles txtBookCode.TextChanged, _
            txtBookTitle.TextChanged, txtBookPrice.TextChanged
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
    End Sub
jskfanAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Correct.  If any of the controls listed (txtBookCode, txtBookTitle, txtBookPrice) have their text changed, then the Control_TextChanged() sub will be run.  The "sender" parameter in the sub will identify which control caused the event to fire.

And yes, you could also run it manually by name:

    Control_TextChanged(Nothing, Nothing)

or

    Control_TextChanged(Nothing, New System.EventArgs)

or actually pass in valid parameters to that sub...

0
 
lostcarparkConnect With a Mentor Commented:
In Visual Basic prior to .NET handlers were associated with controls purely by the name of the handler function. For example, "Textbox1_Change" would automatically be assocated with the change event of Textbox1.

In the .NET languages, that ceases to be the case. Instead they use the "Handles" keyword to determine what control events are handled. While the default handler functions created by double-clicking the control continue the old VB convention, there is no required relationship, and you could call the function anything you want.

In the above example, a singler handler responds to the TextChanged event of three different controls. The handler can then use the "sender" parameter to tell which control raised the event.

This can be really handy if you have a number of controls which do similar things.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Incidentally, you can achieve the same thing without using the "Handles" keyword by using AddHandler():

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler txtBookCode.TextChanged, AddressOf WhatEverYouWantToCallMe
        AddHandler txtBookTitle.TextChanged, AddressOf WhatEverYouWantToCallMe
        AddHandler txtBookPrice.TextChanged, AddressOf WhatEverYouWantToCallMe
    End Sub

    Private Sub WhatEverYouWantToCallMe(ByVal sender As Object, ByVal e As System.EventArgs)
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
    End Sub
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
jskfanAuthor Commented:
my questin is there is no control named Control, so how and when is the below procedure  going to be triggered.?

Private Sub Control_TextChanged
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The NAME of the sub is of no consequence!...

At the end of the sub is this part:

    ... "Handles txtBookCode.TextChanged, txtBookTitle.TextChanged, txtBookPrice.TextChanged"

The "Handles" keyword tells VB.Net that this sub (whatever it's called) should handle the events for the controls listed after it.

You can change the name of the sub and it doesn't matter.

This will behave the exact same way as before:

    Private Sub Idle_Mind_Was_Here(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles txtBookCode.TextChanged, _
            txtBookTitle.TextChanged, txtBookPrice.TextChanged
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
    End Sub
0
 
jskfanAuthor Commented:
so this sub Private Sub Control_TextChanged  is run:
1- if any of the following event is raised
 txtBookCode.TextChanged,txtBookTitle.TextChanged, txtBookPrice.TextChanged
2-or called specifically by its name

Is this how it gets run?




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.