Whenever you write the code logic that may be used for more than one control, it makes sense to try to save time by creating only one procedure that will handle the event and perform that logic. In this tip, I show you a way to share the event logic for multiple controls on a VB.NET form.

Multiple controls in the same code

In order to facilitate development, save time, and make modifications in one place instead of many spots, it's a good idea to implement a procedure that will work the same way for multiple controls and multiple events.

In this example, I'll add three textboxes to the form by adding the following code:

Private Sub TextBoxesChanges(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave

Dim txtBox As TextBox
txtBox = CType(sender, TextBox)
MsgBox(txtBox.Text)

End Sub

The procedure defined handles the Leave event of all TextBoxes. I create a variable to refer to the control, and then I set the variable to the control for which the event has fired. Finally, I display the Text property value of that variable in the message box.

Note: in my example, I use the same type of control (TextBox), but you could have the same functionality for different controls and handle them the same way. You could also handle different events in the same Sub even though in my example I only handled the Leave event.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Comments

1

louis vuitton - 08/06/10

Agree with Landlord sit.Various of online games provided are dynamic sense and free indefinitely.We are looking forward to you to here share the experience of
exciting game competition!

» Report offensive content

1

louis vuitton - 06/08/10

Agree with Landlord sit.Various of online games provided are dynamic sense and free indefinitely.We are looking forward to you to here ... more

Log in


Sign up | Forgot your password?

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!