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

Related links

Comments

1

krishna - 17/04/08

i want more syntax of vb.net

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

krishna - 17/04/08

i want more syntax of vb.net ... more

Log in


Sign up | Forgot your password?

  • Staff Share a keyboard and mouse with Synergy

    Even in the era of virtualization, many IT pros (including myself) have a small army of computers sitting on, under, and around their desks. Read more »

    -- posted by Staff

  • Staff Android devs less than gruntled

    Yet more discouraging news on the Android front. Having hacked off its developer community by releasing updated SDKs to just a small group of chosen devs, Google has now given the brush-off to a petition that called for more to be given to the wider community. Read more »

    -- posted by Staff

  • Staff VMware shows how not to do it

    As a developer there will be a time when you ship a bug -- be it a stub that you left in, or a flaming, crashtastic segfault. The next time this happens and your bosses come baying for blood, point them in the direction of VMware, who this week gave the developer world a great example of how to ship a showstopper bug. Read more »

    -- posted by Staff

What's on?

  • Club Builder: Captain Obvious vs the Crackpots

    In the case of the bleeding obvious, IBM says open source needs good designers; a claim is made that China can activate your phone to snoop on you; and we take a look at the Defcon conference.