?
Solved

Using Handles in VB.Net

Posted on 2006-05-13
6
Medium Priority
?
242 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:jskfan
  • 3
  • 2
6 Comments
 
LVL 5

Assisted Solution

by:lostcarpark
lostcarpark earned 400 total points
ID: 16676200
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16676346
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
 

Author Comment

by:jskfan
ID: 16690178
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16690344
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
 

Author Comment

by:jskfan
ID: 16692170
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 16692942
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Starting up a Project
Screencast - Getting to Know the Pipeline

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question