Event handling with Visual Basic

Event-driven programming changed my life.  This may sound like a spurious claim designed to grab your attention but it’s entirely true.  Before I’d developed in Visual Basic 3.0, I had developed in a number of procedural languages such as CPascal and FORTRAN.  All of which I enjoyed immensely.  However developing in Visual Basic 3.0 was like a breath of fresh air, an absolute revelation.  It looked beautiful and the debugging tools were immense.  However it was the WYSIWYG development environment and the ease of attaching code to Events that really made me say “Wow”.  If I follow this through to its natural conclusion, it was Events driven programming that really made me decide to become a software developer.  And I did!  It changed mine and many millions of lives.

So what exactly is an Event?  An Event (in Visual Basic terms) is a reaction to a user action.  A user may click a button and Visual Basic will fire an event.  You can easily insert your own code in this Click Event, and this code will be called (or fire) when the user clicks the button.  All controls have their own events.  Hovering a mouse over a label or selecting an item in a listbox will fire an event.  The beauty of this for me was the ability of me to quickly and automatically couple my code to the actions of a user.  For example, to attach code to the Button Click Event, I didn’t have to do anything major to tie the code to the event.  I just double clicked on the button in Visual Basic, selected the Event I wanted to code against and inserted my code.  It was so intuitive.   Event driven programming changed the way I thought about software development.  I was coding an application based on what how the user interacted with my application.  I didn’t have to develop any routines to attach code to Events.  All of this was provided out of the box.   It was real Rapid Application Development (RAD was one of the more unfortunate buzz word in those heady days).

This was, of course, possible in procedural programming, but I would have had to do develop code to fire Events myself.  I myself would have to develop all the plumbing.  And my plumbing was never as neat or as efficient as Microsoft’s plumbing.  Microsoft Visual Basic was and remains the most intuitive language on the planet and this is in no large part aided by the ease in implementing Events.  Visual Studio Event handling is conceptually the same as it was back then.

Let’s stop the chat and start the fun.  Open Visual Studio and create a New Project called Events.

Add a new Button to Form1, change the Name of the button to ButtonClick and change the Text of the button to “Click Me”.  In addition, add a Textbox to the form and rename the TextBox to TextBoxFocus.

visual basic form design view

Now, with your left mouse button, double click on the Button.

You should see the following block of code:

Private Sub ButtonClick_Click(ByVal sender As...

End Sub

This is the Click Event for the Button.  Every time the user clicks the button, any code you add to this event will be called.  The naming convention for Events is typically ControlName_Event – in this case ButtonClick_Click.

Between the Private Sub… and End Sub line, type the following:

MessageBox.Show("Button Clicked")

Go back to the Design View of the Form; you should have two tabs open titled “Form1.vb” and “Form1.vb [Design]” – click the [Design tab] so you can see the following again:

visual basic form design view

Double click the TextBox

You should be taken to the following block of code:


Private Sub TextBoxFocus_TextChanged(ByVal ...

End Sub

This Event, the TextChanged Event, is fired when the text in the textbox is changed. We don’t want to use the TextChanged event in this tutorial though; we want to use the GotFocus Event. This event is fired when the cursor appears in the textbox – it is said to have focus.  To access this Event click the following Dropdown:

events

This Dropdown lists all the Events available for that control.  Select the GotFocus event.

The following code should be created.


Private Sub TextBoxFocus_GotFocus(ByVal sender...

End Sub

In between the Private Sub line and the End Sub line, enter the code:


TextBoxFocus.Text = "Textbox has focus"

Go back to the Events dropdown list and select the LostFocus event.

A new event should be created, TextBoxFocus_LostFocus.  This event fires when the cursor leaves the textbox- it loses focus.

Add this line of code to the event:


TextBoxFocus.Text = "Textbox lost focus"

Finally, delete the empty TextChanged event code.  Your code window should look as follows:


Public Class Form1

Private Sub ButtonClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClick.Click
     MessageBox.Show("Button Clicked")
End Sub

Private Sub TextBoxFocus_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxFocus.GotFocus
     MessageBox.Show("Textbox has focus")
End Sub

Private Sub TextBoxFocus_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxFocus.LostFocus
     TextBoxFocus.Text = "Textbox lost focus"
End Sub

End Class

Run your application.  Click the button.   You should see a MessageBox appear saying “Button Clicked”.   This is the code you entered in the Click Event.  Click the textbox.  Do you see the text “Textbox has focus appear”?  This is the code you added to the GotFocus Event.  Click the button again.  Did you notice the textbox text changes again?  The events are firing as you interact with the form.  With three lines of code you are capturing user input and reacting accordingly.  Play around with other events, see what fires them.   You are handling events with Visual Basic.

Check out the video to really get to grips with Event Based Programming in Visual Basic.