Classic VB - What are events and how do I use them?
One of the keyfeatures of Visual Basic is that a lot of it is event-based. This isn't the case with many other languages around, so why have events?
The basics
As we know, VB is probably the easiest programming language out there. Much of this is thanks to events. Events allow you to have a very easily understandable place to put your code into. For example a Click event can be found in most of the controls. In many other languages you'd have much harder time figuring out how to do even this basic thing. Of course this makes it hard to use the mouse in the way you'd like to use it in special occasions, but you save a lot of programming time as the Click event is there ready for you.
Under the hood, events are raised by a certain part of the code. VB runtime does quite a bit of the work and you can't effect much of it. You can create and raise the events of your own though. You could do this for a regular form, but often there is no good reason for this as you can do it in other way. UserControls and Class Modules are a different story: you can make events in both and they can be very useful when used well.
Events and UserControls
In the general declarations part of the code, you can declare events as follows:
VB Code:
Public Event Click()
Public Event DblClick()
Public Event KeyDown(KeyCode As Integer, Shift As Integer)
Public Event KeyPress(KeyAscii As Integer)
Public Event KeyUp(KeyCode As Integer, Shift As Integer)
Public Event MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Public Event MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Public Event MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
These are some of the most common ones to include in a usercontrol. Now, to make something happen, you simply do like this:
VB Code:
Private Sub UserControl_Click()
RaiseEvent Click
End Sub
Things are pretty straightforward in the case of a Click. However, you can use events more creatively as well: you could have a control that follows a status of a progress and fires an event each time something happens. RaiseEvent would then be in the middle of a "regular" code and it wouldn't just be redirecting an existing event of the UserControl.
Here is some more example code to clarify the passing of variables:
VB Code:
Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
RaiseEvent KeyDown(KeyCode, Shift)
End Sub
Events and Class Modules
You can create events for Class Modules as well. The method is the same as with usercontrols: Public Event declaration in the general declarations portion of the code and the RaiseEvent to make the event fire. The only difference comes with the way you include a class module elsewhere (in a form or a module): you must use WithEvents keyword to be able to use the events. Of course you also don't have any premade events as class modules are objects in the memory only and do not have a graphical interface (unless you "manually" create one).
VB Code:
Dim WithEvents MyClass As clsMyClass
Private Sub Form_Load()
' this is a recommended way to initialize a class before you use it
Set MyClass = New clsMyClass
End Sub
Private Sub Form_Unload()
' clear memory after use
Set MyClass = Nothing
End Sub
' and then the class events!
Private Sub MyClass_JobDone()
' and some code here
' this would not work if there was no WithEvents
End Sub
That ought to be about everything you need to know about events. They make things easy to implement, so use them when they seem fit.
Comments and feedback
As you can't leave questions and thanks and the like in the FAQ section, you can send me a private message instead. However, you are welcome to reply if you have something to add.