1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 6 - Using Decision Structures - Part 1

By Dan Mabbutt, About.com

4 of 6

Responding to Events in Code

For example, if you add a button to a form and then check out the form code, you'll see something that looks like this (lines are continued to make them shorter for the web page display):

~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Button1_Click( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Button1.Click
~~~~~~~~~~~~~~~~~~~~~~~~~

This means that the Button1_Click subroutine will handle the Button1.Click event. Button1 is an object on the form. Click is a method of that object. The two together are called the Button1.Click event.

Now ... suppose that you wanted the same code to handle several different events (I'll give you an example where this would happen just a little later). You could simply code,

~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub MySubroutine( _
  ByVal sender As System.Object, _
  ByVal e As System.EventArgs) _
  Handles Object1.Event1,
      Object2.Event2,
      Object3.Event3
~~~~~~~~~~~~~~~~~~~~~~~~~

When would you ever want to do this? Perhaps you want to create your own calculator program? The code that handles all the key clicks might be the same. I've written an article that shows you exactly how this works. Although it's an advanced topic, you might want to check out User Control Components in VB.NET.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Using Decision Structures - Part 1

©2009 About.com, a part of The New York Times Company.

All rights reserved.