VB6 Tutorial 26: Common Events

Posted On 2018-04-08 by VB6Boy
Keywords:
Tags: VB6 Tutorials Windows
Views: 47

Title Uploaded Size
ASCII_Converter.zip 4/8/2018 5:53:46 AM 1,227
GotFocus_LostFocus.zip 4/8/2018 5:54:14 AM 1,054
Make_Shortcut_Key_1.zip 4/8/2018 5:54:29 AM 1,404
Make_Shortcut_Key_2.zip 4/8/2018 5:54:41 AM 1,223


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

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:

  • 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.

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

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

VB6Boy has posted a total of 71 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:23.20.165.182

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links