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 (I’m showing my age here…), I had developed in a number of procedural languages such as C, Pascal and Fortran which I enjoyed of course.  However developing in Visual Basic 3.0 was like a breath of fresh air.  It looked beautiful, the debugging tools were powerful but it was the Drap and Drop WYSIWYG Development Environment and the ease of attaching Events that really made me say “Wow”.  If I follow this through to it’s natural conclusion, it was Visual Basic Events that made me decide to become a software developer.  And I did!  The reason I am sitting here right now is largely because of the beauty and simplicity of developing applications using Event based Programming.  See, it did change my and many millions of others 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, hover their mouse over a label or select an item in a listbox.  Whatever the action, Visual Basic will fire an event and allow you to easily insert your own code to react to this 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, when a user clicked a button, I could fire a certain block of code.   I didn’t have to do anything 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.  Visual Basic Events changed the way I thought about programming.  I was coding an application based on what the user would use, and nothing else.  I didn’t have to develop any routines to simulate events.  It was provided out of the box.   The whole framework.  It was real Rapid Application Development (RAD was an unfortunate buzz word in those heady days).

This was of course possible in procedural programming, but I would have had to do develop something that would 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.  C# and the rest are merely followers.

What came later in version 4.0 or 5.0 (I forget exactly) was the ability to define and raise your own events, which is a neat tool to have in the toolbox.  But that’s for the more advanced users covered later in these tutorials.  In this tutorial we’ll be dealing with out of the box events, typically raised when a completes an action with an out of the box control, and how to insert your code to react to these events.

Now for the fun part.  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, rename the TextBox to TextBoxFocus.

Now, 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, this event will be called.  The naming convention for Events is typically convention Control_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 that sat “Form1.vb” and “Form1.vb [Design]” – click the [Design tab]).

Double click the TextBox

You should be taken to the following block of code


Private Sub TextBoxFocus_TextChanged(ByVal ...

End Sub

This code is fired when the TextBoxFocus text is changed, the TextChanged event, indicated by the name TextBoxFocus_TextChanged. For the purposes of this exercise however, we don’t want to use the TextChanged event. We don’t want to use the GotFocus Event. To access this event click the following Dropdown

events

This Dropdown lists all the events for that control.  You can select a new control in the left hand dropdown if you wish, but I’d like you to 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 and select the LostFocus event

A new event should be created, TextBoxFocus_LostFocus

Add this line of code to the event


TextBoxFocus.Text = "Textbox lost focus"

Delete the empty TextChanged event code, and 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.  Click the button.  You should see the messagebox you entered in the event.   Click the textbox.  Now click the button again.  Have you noticed the text change?  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.