VB.Net - SPLessons

VB.Net Event Handling

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Event Handling

VB.Net Event Handling

shape Description

Event Handling means performing  some actions by using events. By using Event Handling, we can perform Validations in windows application which is a built in feature in the web application.For Web applications, we have several controls which are used to perform validations. But there are no controls in windows applications. By using this Event Handling Mechanism, we can achieve validations in Windows Application also.There are two Types of Events as follows.

Mouse Events

shape Description

The Events which are raised due to Mouse are called as Mouse Events.The following table shows you Some of the Mouse Events.
Event Description
MouseUp Raised when the Mouse pointer over the control
MouseDown Raised when the Mouse button released
MouseEnter Raised when the Mouse pointer enters the control
MouseHover Raised when the Mouse pointer moves on the control
MouseLeave Raised when the Mouse pointer leaves the control
The following table shows you some of the Properties of the Mouse.
Properties Description
Click Indicates the Number of Clicks
X Indicates the x-Coordinates of the mouse click
Y Indicates the y-Coordinates of the mouse click
Buttons Indicates the mouse button pressed

Keyboard Events

shape Description

The Events which are raised due to Keyboard are called as KeyBoard Events.The following table shows you Some of the KeyBoard Events.
Event Description
KeyUp Raised when the Key is released
KeyDown Raised when the Key is pressed down
KeyPress Raised when the Key is pressed
The following table shows you some of the Properties of the Mouse.
Properties Description
KeyData Used to store the Key Data for the Event
KeyCode Used to store the Key Code for the Event
Handled Used to check whether the event is handled or not
KeyValue Used to store the Key Value for the Event
KeyChar Used to store the Character related to that key which is pressed for the Event

shape Example

Take an Example. We have to create a Registration Form.We have to implement the following conditions that mean validations. Follow the below steps to know how to perform Event Handling.
  • Open Visual Studio 2013. Go to File  and then New, and then Project. Then a new window will be open. Select Visual Basic, Windows Application and also give the Name for your project and click on Ok.
  • Then, the new Windows Application will be created.
  • Now design the form as shown in the below figure.
Change the properties of the every control in the above diagram as shown in the below table.
Control Property
Textbox1 Id as  txtName
Textbox2 Id as  txtMobile
Textbox3 Id as  txtEmail
Textbox4 Id as txtCity
Label1 Id as  lblName Text as Name
Label2 Id as  lblMobile Text as Mobile No
Label3 Id as  lblEmail Text as Email Id
Label4 Id as lblCity Text as City
Button Id as btnSubmit Text as Submit
Write the following code. [vbnet] Public Class Form1 Private Sub txtName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtName.KeyPress If Not (Asc(e.KeyChar) = 8) Then If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90)) Then e.KeyChar = ChrW(0) MessageBox.Show("Enter only characters") e.Handled = True End If End If End Sub Private Sub txtMobile_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtMobile.KeyPress If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then MessageBox.Show("Please enter numbers only") e.Handled = True End If End Sub Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click MessageBox.Show("Successfully submitted") End Sub Private Sub txtCity_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCity.KeyPress If Not (Asc(e.KeyChar) = 8) Then If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90)) Then e.KeyChar = ChrW(0) MessageBox.Show("Enter only characters") e.Handled = True End If End If End Sub End Class [/vbnet] Output: If you are trying to enter any number in Name or City filed. Then, you will get the following error. If you are trying to enter any character in Mobile No filed. Then, you will get the following error. If you enter all the fields successfully. Then you will get the following message.

shape Programming Tips

  • Before developing an application like forms, then  developer need to have an knowledge regarding toolbox.
  • Validation and logic of an object gives a good effect.
  • More errors will give more knowledge while debugging.