Computer Programming web Web programming Tips



Visual Basic 6.0. Form Events - VB code example

By Sergey Skudaev


Visual Basic form events

Figure 1. Form Events. Hover mouse over a thumbnail to see a large image.

In the lesson 1 Start VB you have learned how to use Visual Studio to create a simple VB project. In the lesson 2 you will learn form events and properties. I will show you what events form has and how to use this events in Visual Basic application. - VB code example is provided.

Open Visual Basic development environment and select Standart.EXE project from the New Project window. The form1 will be displayed. Double click the (Name) line on the Properties Form1 window and type your form name frmMain. Double click the Caption line and type frmMain. The frmMain caption will be displayed on the form title bar.

Select Project Project1 in the Project Project1 window in the righ upper corner. Double click the (Name) line in Properties Project1 window in the right lower corner and type your project name FormEvent.



Select image control from toolbox on the left and draw the image on the frmMain form. See Figure 2.

Visual Basic form events

Figure 2. frmMain

Select image and find DragMode line in the Properties image1 window. Set DragMOde to 1-Automatic. It allows you to drag image around the form. Select DragIcon properties and click browser button on the right. In Load Icon dialog go one level up and select the Common folder. In the common folder select Graphics, Icons and select any icon you like. I selected key icon, This icon will be displayed when user drag the image.

Double click the form. The FormEvent frmMain code window will be displayed. Copy the folowing variable declarations and paste them under the Option explicit text

Option Explicit
Public mousemovecount As Integer
Public mousedowncount As Integer
Public mouseupcount As Integer
Public singleclickcount As Integer
Public doubleclickcount As Integer
Public lostfocuscount As Integer
Public clickcount As Integer
Public dragovercount As Integer
Public keydowncount As Integer
Public keypresscount As Integer
Public paintcount As Integer
Public deactivatecount As Integer

These variables will be used to count each event. Purpose of this counting you will understand later. It is important to know the sequence of events when form is loaded or unloaded. Form initialization is the first event that occurs when form is loaded, then form load event occurs, then paint event, then got focus, the last event is activated. If you open any other window and close it, the form will redraw itself and paint event occurs again. If you want to change form appearance (backcolor or text color etc.) then the best way is to place the code performing this change inside the form_activate procedure. If you write frmMain.BackColor = &H8000000A in the form_initialize event procedure, it will not work, because form is not loaded and backcolor property does not exist yet.

I initialized all count variables on form load event. To see what event occurs, we place Message Box procedure in each event procedure with a message, which tells which event occurs. For example, in Form_Activate() procedure we place message "Form Activate event" in Form_Load() procedure "Form load event" and so on. Some time, we have to disable message box on an event to be able to see the next event. For example, when you what to click form, you move mouse and mouse move event occurs. To see click event we have to disable msgbox on mouse move event if it occurred more than once. The code will be like that:

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

If mousemovecount = 0 Then   'if event does not occur yet then display message
        MsgBox "Form MouseMove event...", vbInformation, "Form Events"
       mousemovecount = mousemovecount + 1
Else         'if it occurs at least once (mousemovecount) = 1 do not display it.
   Exit Sub
End If
End Sub

If you want to see message on double click event, then you have to disable message on Click event, on mouse down event,and on mouse up event. That is why we need count variables for almost each event. We count each event and disable the event message after seeing it for couple of time.

Key Down, Up and Press events occurs when user hit a key. Notice, that KeyDown event and KeyPress are different. KeyDown event has KeyCode as an argument and KeyPress has ASCII code as an argument. You can view which key 'A' or 'W' user pressed using Chr(KeyCode) function:

MsgBox "Form KeyDown event..." & Chr(KeyCode) & " Key pressed", vbInformation, "Form Events"

but you cannot know was it lower case or upper case. ASCII code is different for Upper case and lower case. That is why KeyPress event allows you to know if user press lover case 'a' or Upper case 'A'. Ampersand character is used in Visual Basic to connect strings of text. For example if string1="Hello" and string2 = "World!" then to view message box "Hello World" you have to write the following code:

msgbox string1 & " " & string2

Download the complete code for the FormEvents project

Paste code for each event procedure in VB code window. Before you save the project go to Project menu and select add form option. Select standard form. Add it to project and name frmDummy. Save FormEvents project in formevent folder.

Visual Basic form events VB Project

Figure 3. Form Events VB Project

This form is needed to observe some events. When user clicks frmDummy form, while frmMain form is active, lost focus event occurs , then deactivate event occurs for frmMain form. When user clicks frmMain form again, got focus and activate events occur for frmMain form.

frmDummy form is loaded when double click event occurs for frmMain form:

Private Sub Form_DblClick()

       MsgBox "Form DoubleClick event...", vbInformation, "Form Events"
       frmDummy.Show

End Sub

frmDummy.show method will display frmDummy form when user double clicks frmMain form.

Visual Basic form events: show method

Figure 4. Dummy form

Now project is ready to test. Run it and observe the form events. Try move mouse, press mouse down and hold it, release mouse button, click frmMain. Try to resize form. Try press keys. (Upper case and lower case). Double click frmMain form to load frmDummy form. Click frmDummy form. Click frmMain form. At the end click 'X' in the right upper corner of frmMain form to close it. The QueryUnload event occurs. On this event form scope objects should be set to nothing to free memory. Then form unload and form terminate events occur.

Try to write some procedures and place them in different event subroutines. Observe what happens.

Good Luck!.

My eBooks on Amazon.com



Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
Is ice cream hot or cold?
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics