Search Tools Links Login

VB6 Tutorial 26: Common Events


This VB6 lesson is about 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:

Quickhint

  • Change event is not raised when you select an item from the ComboBox control.
  • 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.

The attached sample project GotFocus_LostFocus demonstrates the focus-related events. Note: this project makes use of the debug window to show the output. This window will appear at the bottom of your IDE, and will show any output from debug.print statements.

If the windows is not visible, press Control+G to toggle it on. To make it disappear, press Contrl+G again.

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.

The attached sample project, ASCII_Converterdemonstrates the Keypress event. Note that the project also uses 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.

See the two attached sample projects (Make_Shortcut_Key_1 and Make_Shortcut_Key_2) to see examples of making your own shortcut keys.

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

MouseDown & MouseUp

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

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

About this post

Posted: 2018-04-08
By: vb6boy
Viewed: 1,468 times

Categories

Visual Basic 6

VB6 Tutorial

Attachments

Make_Shortcut_Key_2.zip
Posted: 4/8/2018 5:54:41 AM
Size: 1,223 bytes

Make_Shortcut_Key_1.zip
Posted: 4/8/2018 5:54:29 AM
Size: 1,404 bytes

GotFocus_LostFocus.zip
Posted: 4/8/2018 5:54:14 AM
Size: 1,054 bytes

ASCII_Converter.zip
Posted: 4/8/2018 5:53:46 AM
Size: 1,227 bytes


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.

ADODB.Connection error '800a0e79'

Operation is not allowed when the object is open.

/assets/inc/inc_footer.asp, line 37