Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VB.Net - Event handling


May 13, 2021 vb.net


Table of contents


Events are basically user actions, such as keystrokes, clicks, mouse movements, etc., or certain events, such as system-generated notifications. Applications need to respond to events when they occur.

Clicking a button, or entering some text in a text box, or clicking a menu item, are examples of events. An event is an action that calls a function or that could cause another event.


An event handler is a function that indicates how to respond to an event.

Vb. N et is an event-driven language. There are two main types of events:

  • Mouse events for mouse events

  • Keyboard events


Handle mouse events

Mouse events occur with mouse movement forms and controls. H ere are a variety of mouse events related to the Control class:

  • MouseDown - occurs when the mouse button is pressed

  • MouseEnter - Occurs when the mouse pointer enters the control

  • MouseHover - Occurs when the mouse pointer hovers over the control

  • MouseLeave - Occurs when the mouse pointer leaves the control

  • MouseMove - When the mouse pointer moves over the control

  • MouseUp - Occurs when the mouse pointer is above the control and the mouse button is released

  • MouseWheel - It happens when the mouse wheel moves and the control has focus

The event handler for the mouse event gets a parameter of one type, MouseEventArgs. T he MouseEventArgs object is used to handle mouse events. I t has the following properties:

  • Buttons - means press the mouse button

  • Clicks - Shows clicks

  • Delta - represents the number of positioning slots that rotate the mouse wheel

  • X - Indicates the x coordinates of the mouse click

  • Y - represents the y coordinates of the mouse click


Example

Here's an example of how to handle mouse events. F ollow these steps:

  • Add three labels, three text boxes, and a button control to the form.

  • Change the text properties of the label to - customer ID, name, and address, respectively.

  • Change the name properties of the text box to txtID, txtName, and txtAddress, respectively.

  • Change the text property of the button to Submit.

  • Add the following code to the code editor window:

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = "tutorialspont.com"
   End Sub

   Private Sub txtID_MouseEnter(sender As Object, e As EventArgs)_
        Handles txtID.MouseEnter
      'code for handling mouse enter on ID textbox
      txtID.BackColor = Color.CornflowerBlue
      txtID.ForeColor = Color.White
   End Sub
   Private Sub txtID_MouseLeave(sender As Object, e As EventArgs) _
        Handles txtID.MouseLeave
      'code for handling mouse leave on ID textbox
      txtID.BackColor = Color.White
      txtID.ForeColor = Color.Blue
   End Sub
   Private Sub txtName_MouseEnter(sender As Object, e As EventArgs) _
       Handles txtName.MouseEnter
      'code for handling mouse enter on Name textbox
      txtName.BackColor = Color.CornflowerBlue
      txtName.ForeColor = Color.White
   End Sub
   Private Sub txtName_MouseLeave(sender As Object, e As EventArgs) _
      Handles txtName.MouseLeave
      'code for handling mouse leave on Name textbox
      txtName.BackColor = Color.White
      txtName.ForeColor = Color.Blue
   End Sub
   Private Sub txtAddress_MouseEnter(sender As Object, e As EventArgs) _
      Handles txtAddress.MouseEnter
      'code for handling mouse enter on Address textbox
      txtAddress.BackColor = Color.CornflowerBlue
      txtAddress.ForeColor = Color.White
   End Sub
   Private Sub txtAddress_MouseLeave(sender As Object, e As EventArgs) _
        Handles txtAddress.MouseLeave
      'code for handling mouse leave on Address textbox
      txtAddress.BackColor = Color.White
      txtAddress.ForeColor = Color.Blue
   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) _
       Handles Button1.Click
      MsgBox("Thank you " & txtName.Text & ", for your kind cooperation")
   End Sub
End Class


When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed:


VB.Net - Event handling

Try typing text in the text box and check the mouse event:


VB.Net - Event handling


Handle keyboard events

Here are a variety of keyboard events related to theControl class:

  • KeyDown - Occurs when a key is pressed and the control has focus

  • KeyPress - Occurs when a key is pressed and the control has focus

  • KeyUp - Occurs when the key is released when the control has focus


The event handler for KeyDown and KeyUp events gets a parameter of type KeyEventArgs. T his object has the following properties:

  • Alt - It indicates whether the ALT key is pressed/

  • Control - It indicates whether the CTRL key is pressed

  • Handled - It indicates whether the event was handled

  • KeyCode - The keyboard code that stores events

  • KeyData - Stores keyboard data for events

  • KeyValue - The keyboard value that stores the event

  • Modifiers - indicates which modification key to press (Ctrl, Shift and/or Alt)

  • Shift - indicates whether the Shift key is pressed


The event handler for KeyDown and KeyUp events gets a parameter of type KeyEventArgs. T his object has the following properties:

  • Handled - Indicates KeyPress event handling

  • KeyChar - Stores characters that correspond to the key pressed

Example

Let's move on to the previous example to show how to handle keyboard events. T he code verifies that the user enters some numbers for their customer ID and age.

  • Add a label with a text property called Age, and add a corresponding text box called txtAge.

  • Add the following code to handle the KeyUP event for the text box txtID.

    Private Sub txtID_KeyUP(sender As Object, e As KeyEventArgs) _
       Handles txtID.KeyUp
       If (Not Char.IsNumber(ChrW(e.KeyCode))) Then
          MessageBox.Show("Enter numbers for your Customer ID")
          txtID.Text = " "
       End If
    End Sub
    
  • Add the following code to handle the KeyUP event for the text box txtID.

    Private Sub txtAge_KeyUP(sender As Object, e As KeyEventArgs) _
       Handles txtAge.KeyUp
       If (Not Char.IsNumber(ChrW(e.keyCode))) Then
          MessageBox.Show("Enter numbers for age")
          txtAge.Text = " "
       End If
    End Sub
    


When you execute and run the code above using the Start button on the Microsoft Visual Studio toolbar, the following window is displayed:


VB.Net - Event handling


If you leave the text of theage or ID blank, or enter some non-digital data, a warning message box appears and clears the text:


VB.Net - Event handling