Visual Basic

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

Handling Events in VB.NET

The "e" argument - and an example

By Dan Mabbutt, About.com

The other argument, e, contains information you need to process the event. The information available depends on the type of event that was raised. Since the members available through the "e" argument depend on the kind of action that raised the event, it is important (particularly for VB 6 programmers who are learning VB.NET) to use an event that provides the information you're interested in. If you need mouse-type information, you must use a mouse-type event, and so on. For a sample of the difference, the next illustration shows you the members available using the e argument with the Click event versus the MouseClick event.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

For a more practical example of this, I'd like to thank (again ... for the umpteenth time) frequent contributor Dr. Peter Ingerman for suggesting this topic with a question he sent to me in an email. Dr. Ingerman asked, "What I'm trying to do is to determine whether the user clicked on the PictureBox in the upper third of the picture, the middle third of the picture, or the bottom third of the picture."

Here's the code that does the trick!

Private Sub PictureBox1_MouseDown( _
   ByVal sender As Object, _
   ByVal e As System.Windows.Forms.MouseEventArgs) _
   Handles PictureBox1.MouseDown
   Dim myPicBox As PictureBox = sender
   Select Case e.Y / myPicBox.Height
      Case Is > 2 / 3
         Debug.WriteLine("It's in the bottom third")
      Case Is > 1 / 3
         Debug.WriteLine("It's in the middle third")
      Case Else
         Debug.WriteLine("It's in the top third")
   End Select
End Sub

Explore Visual Basic

By Category

About.com Special Features

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Handling Events in VB.NET

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

All rights reserved.