COMMON EVENTS

The common events that give life to event driven programming. 


Change

Visual Basic fires Change event of a control when the contents of that particular control change. Change event is especially useful for TextBox. When you type something in the TextBox, the Change event of TextBox is raised. 

Example:
Private Sub txtField_Change()
    txtField.Text = ""
End Sub

In this example, whenever you type something, the Change event is triggered and the text field is cleared. That means you cannot write anything in the TextBox.

The other important points about this event are:
  • ComboBox, both scroll bars, PictureBox, DriveListBox and DirectoryListBox also raise a Change event.
  • ComboBox raises Change event when you type in the editable area of the control. 


    Note: 
    1. Change event is not raised when you select an item from the ComboBox control.
    2. Change event is not raised when you programmatically change the text of a an item of a  ComboBox control.
    • Scroll bar controls raise Change event when the user clicks on the arrows.
    • When you set a directory from DirectoryListBox by double-clicking on that particular item  or when you choose a drive from DriveListBox, Change event is raised.
    • Change event fires when the content of the control is changed through code. The below example clarifies this:
    Private Sub cmdButton_Click()
        Text1.Text = ""
    End Sub
    ________________________________________
    Private Sub Text1_Change()
        MsgBox "change event fired"
    End Sub


     GotFocus 

    The lines of code in the GotFocus Event procedure of the object is executed, when the object gets the focus.

    LostFocus

    The lines of code in the GotFocus Event procedure of the object is executed, when the object losts the focus.

    Download the following program:

    Go here for similar code samples: VB6 demo applications


    KeyPress 

    When you press any key from the keyboard, the KeyPress event of the object is invoked. 

    Example: Detects which key has been pressed.

    Private Sub txtField_KeyPress(KeyAscii As Integer)
        Dim key As String

        key = Chr(KeyAscii)
        Print "You have entered  " & key

    End Sub

    KeyAscii in the procedure's paremeter is the ASCII code of the key you have pressed.

    The KeyPress event is clarified in the following sample application.

    Download sample program:  ASCII Converter

      To understand the above sample application, you need to learn about the 'Chr function'. This function converts the character value of an ASCII code.



      KeyDown and KeyUp

      The KeyDown event fires when you press the key and the KeyUp event is raised when you release the key. The KeyDown and KeyUp event procedures have two parameters, KeyCode and Shift. The KeyCode parameter is the code for the pressed key and the Shift parameter indicates the state of the Ctrl, Shift and Alt keys.

      Example: Clear TextBox pressing F5. 

      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
          If KeyCode = vbKeyF5 Then
              Text1.Text = ""
          End If
      End Sub

      Example: Change form's background color using shortcut keys.

      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
          If KeyCode = vbKeyG And Shift = vbCtrlMask Then
              Form1.BackColor = vbGreen
          ElseIf KeyCode = vbKeyD And Shift = vbShiftMask Then
              Form1.BackColor = vbWhite
          End If
      End Sub

      The another symbolic constant value for the Shift parameter is vbAltMask.



      Download the following programs:  


      MouseMove

      When you move the mouse over an object, the MouseMove event is raised.

      Example: The background color of the PictureBox is set to black when you move the mouse over it.
      Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
          Picture1.BackColor = vbBlack
      End Sub



      Example: Shows the mouse pointer position on the form.
      Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
          Label1.Caption = X
          Label2.Caption = Y
      End Sub


      Download this sample: Example of MouseMove


        MouseDown & MouseUp

        When the mouse is clicked, the MouseDown event is raised.
        When the mouse is released, the MouseUp event fires.

        Download sample program: Example of MouseDown & MouseUp
                                                                                    
          The MouseDown, MouseUp and MouseMove events receive the same paremeters --Button, Shift, X, Y. Button indicates the state of mouse buttons. Shift indicates the states of Ctrl/Shift/Alt keys, and X is x-coordinate, Y is y-coordinate.

          Example: Shows which mouse button has been pressed.
          Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
              If Button = vbLeftButton Then
                  Label3.Caption = "Left"
              ElseIf Button = vbRightButton Then
                  Label3.Caption = "Right"
              ElseIf Button = vbMiddleButton Then
                  Label3.Caption = "Middle"
              End If
          End Sub

          Download sample program : Mouse Capture