Introduction to VB.Net Events
Visual Basic .Net is an object-oriented, multi-paradigm based programming language that is implemented on the .Net framework. This was launched in 2002 by Microsoft which came as a successor to the most popular Visual Basic language. The IDE or the integrated development environment provided by Microsoft for tackling and handling this Visual basic language is Microsoft Visual Studio. The visual studio express and the community version are the non-paid unlicensed versions of the tool which therefore forms the part of the freeware. The freeware includes a command-line compiler SDK which is called the vbc.exe. Mono also makes use of the command line VB .NET compiler. One of the very prime features of the visual basic language is that it makes use of the statements which is used to specify actions. In this article, we will discuss about VB.Net Events in detail.
Even when you visualize a visual studio project as procedural series which are executed in a sequence but in reality, they all form a part of the events and therefore they are event-driven which means that the flow of execution is only determined by all the external occurrences happening around which are also called as events. They are basically user actions such as keypress, mouse-related movements, clicks, etc. or it is also used for doing some occurrences such as system-oriented and generated notifications. Also, the applications are also required to respond to all the events whenever they occur. This includes clicking on a home button or entering some form of a text in the text box, clicking on the menu item. They all form part of event examples. An event can be defined to be an action which calls another function or causes another event to get triggered. Handlers, on the other hand, are functions that tell us how to respond to the event.
Types of VB.Net Events
Below are the Two types of VB. Net event:
1. Mouse-Based Events
Mouse events occur when the mouse makes any movement in controls or the forms. The following are some of the mouse-related events which are related to a control class.
i. MouseEnter
This event gets triggered when the pointer of the mouse enters the control.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.MouseEnter
Program Body
End Sub
Example:
//defining a subroutine
Private Sub MouseEnter(sender As ObjectType, e As EventArguments)
Handles abc.MouseEnter
//writes the code for handling mouse enter on abc textbox
abc.BackColor = Color.Blue
abc.ForeColor = Color.Red
End Sub
ii. MouseDown
This event gets triggered when the mouse button is pressed and the cursor is brought towards the downside of it.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.MouseDown
Program Body
End Sub
Example:
//defining a subroutine
Private Sub MouseDown(sender As ObjectType, e As EventArguments)
Handles abc.MouseDown
//writes the code for handling mouse down
abc.BackColor = Color.Blue
abc.ForeColor = Color.Red
End Sub
iii. MouseUp
This event gets triggered when the mouse pointer is over the control which is handling it and the already pressed mouse button is released.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.MouseUp
Program Body
End Sub
Example:
//defining a subroutine
Private Sub MouseUp(sender As ObjectType, e As EventArguments)
Handles abc.MouseUp
//writes the code for handling mouseUp
abc.BackColor = Color.Blue
abc.ForeColor = Color.Red
End Sub
iv. MouseLeave
This is the event that gets triggered when the mouse pointer leaves the control.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.MouseLeave
Program Body
End Sub
Example:
//defining a subroutine
Private Sub MouseLeave(sender As ObjectType, e As EventArguments)
Handles abc.MouseLeave
//writes the code for handling mouseLeave for abc textbox
abc.BackColor = Color.Blue
abc.ForeColor = Color.Red
End Sub
v. MouseWheel
This event gets triggered when the wheel of the mouse moves and the control is provided the focus.
Syntax
<element onwheel="mytestScript">
object.onwheel = function(){mytestScript};
object.addEventListener("wheel", mytestScript);
Example:
document.getElementById("DIV").addEventListener("wheel", testFunction);
function testFunction() {
this.style.fontSize = "70px";
}
2. Keyboard Based Events
These are the events that are triggered when the events are fired upon any action done on the keyboard. This includes actions such as keypress, keydown, enter, etc. Let us study some of the keyboard-based events in detail.
i. KeyDown
This event is a keyboard-based trigger that occurs when a key is pressed towards the downside and the control has focus.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.KeyDown
Program Body
End Sub
Example:
Private Sub KeyDown(sender As Object, e As KeyEventArgs) _
Handles abc.KeyDown
If (KeyDown.IsNumber(Chr(e.KeyCode))) Then
alert.Show("Enter Customer ID number")
abc.Text = " "
End If
End Sub
ii. KeyUp
This event is a keyboard-based trigger that occurs when a key is pressed towards the upside and the control has focus.
Syntax:
private sub func_name(alias as Datatype1, alias as Datatype2)
Handles object.KeyUp
Program Body
End Sub
Example:
Private Sub KeyUp(sender As Object, e As KeyEventArgs) _
Handles abc.KeyUp
If (KeyUp.IsNumber(Chr(e.KeyCode))) Then
alert.Show("Enter Customer ID number")
abc.Text = " "
End If
End Sub
iii. KeyPress
This event gets triggered when a particular key is pressed onto the keyboard.
Syntax:
Public Class class_name
private sub func_name(alias as Datatype1, alias as KeyPressArguments)
Handles object.KeyPress
Program Body
End Sub
Example:
Public Class testKeyPress
Private Sub KeyPress(Value sender As System.Object, Value e As System.Windows.KeyPressEventArguments) Handles abc1.KeyPress
If !(e.KeyChar = Convert.ToChar(10)) Then
MsgBox("key is not pressed ")
End If
End Sub
End Class
Conclusion – VB.Net Events
Events form an essential role in the structure and the material of Visual Basic language which is based on the .Net framework. This language provides a rich set of events that can be used to enhance the coding and scripting capabilities if learned and used in the right way. Different projects make use of different versions of a language as per their requirement and every language version varies with the syntax and the functions/ events which are introduced so it becomes very necessary to use the right set of language which suits your needs and for which you have the skilled resources.
Recommended Articles
This is a guide to VB.Net Events. Here we discuss the introduction and two different types of VB.Net Events with syntax and examples. You can also go through our other suggested articles to learn more –