Form Events and Capabilities VB6

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search

Form Move Event

  • Microsoft VB .NET has a form_move event, but Visual BASIC 6 does not.
  • The Paint event won't detect a window being moved off the screen, but will get triggered when the window is moved back onto the screen. If AutoRedraw is enabled the Paint event is not fired at all.
  • GotFocus/LostFocus Events are not triggered by the form.
  • Form_MouseMove is fired when the mouse cursor is over the form, not including the title bar. However, dragging is done with the title bar.

Your options are:

Subclassing using WM_WINDOWPOSCHANGED

Subclassing can make the Visual Studio IDE unstable. The debugger does not work well when a new WindowProc is installed.

EventVB.dll Custom Functions

First add the EventVB.dll to your project references. It is not included with Visual Studio.

 Option Explicit
 
 Dim WithEvents vbLink As EventVB.APIFunctions
 Dim WithEvents vbWnd As EventVB.ApiWindow
 
 Private Sub Form_Load()
 
 Set vbLink = New APIFunctions
 
 Set vbWnd = New ApiWindow
 vbWnd.hWnd = Me.hWnd
 '\\ Subclass this form
 vbLink.SubclassedWindows.Add vbWnd
 
 End Sub
 
 Private Sub vbWnd_Move(ByVal x As Long, ByVal y As Long, Cancel As Boolean)
 
 '\\ This event occurs when a drag-move completes:
 Debug.Print "Moved to " & x & "," & y
 
 End Sub
 
 Private Sub vbWnd_Moving(ByVal MoveEdges As EventVB.WindowSizingEdges, MoveRectangle As EventVB.APIRect)
 
 '\\ This event occurs during the drag-move
 With MoveRectangle
     Debug.Print "Moving to " & .Left & "," & .Top & " - " & .Right & "," & .Bottom
 End With
 
 End Sub

 

Open a Form From Another Form

Open the form called frmCompanies

 frmCompanies.Show

Open it so that the parent form cannot be clicked. To suspend execution of the first form until after the second form is done with, add the keyword constant vbModal as an argument to the Show method.

 frmCompanies.Show vbModal

The Hide method of a form removes the form from the screen (makes it invisible), but the form still remains in memory.

 frmCompanies.Hide
 Me.Hide
 Hide

You can load a form in memory and not display it.

 Load frmCompanies

Unload form from memory (which also closes it)

 Unload Me

Example

 Private Sub Form_Unload(Cancel As Integer)
   If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Unload Test") = vbNo Then
     Cancel = 1
   End If
 End Sub

 

From Dimensions and Twip Units

The default unit is the twip, which is 1/1440 of an inch for height, width, top, and left.

There are 15 twips per pixel.

Pixels to Twip:

 W = W * Screen.TwipsPerPixelX
 H = H * Screen.TwipsPerPixelY 

Twips to Pixel:

 W = W / Screen.TwipsPerPixelX
 H = H / Screen.TwipsPerPixelY

 

Resizing Forms

  • Width: Specifies the width of the control. The default unit is the twip, which is 1/1440 of an inch.
  • Height: Specifies the height of the control. The default unit is the twip, which is 1/1440 of an inch.
  • Left: Specifies the distance between the internal left edge of a control and the left edge of its containter.The default unit is the twip, which is 1/1440 of an inch.
  • Top: Specifies the distance between the internal top edge of a control and the top edge of its containter.The default unit is the twip, which is 1/1440 of an inch.


  • ScaleWidth: The width of the client area.
  • ScaleHeight: The height of the client area.
  • ScaleLeft: Coordinate horizontal of upper left client area.
  • ScaleTop: Coordinate vertical of upper left client area.


  • ClientWidth:
  • ClientHeight:
  • ClientLeft:
  • ClientTop:

 

Write to control on another form and display

 Dim lblCaught As Label
 Set lblCaught = frmKaching.lblCaught
 frmKaching.WindowState = vbNormal
 lblCaught = "You've Been Naughty!"
 frmKaching.Show

 

Determine is a form has been loaded

The wrong way:

 if form1.visible = true then

The reason:

Due to the way an intrinsic variable instance works in VB, the line above will actually cause form1 to load. Forms are loaded automatically whenever referenced in code, whether loaded visibly or not.

Special thanks to a gentleman named Steve Gerrard who posted a correct sample script on another forum. His example works by going through a list of loaded forms and matching by the form name you are looking for.

 Public Function FormIsLoaded(FormName As String) As Boolean
   Dim oFrm As Form
   For Each oFrm In Forms
     If oFrm.Name = FormName Then
       FormIsLoaded = True
       Exit For
     End If
   Next oFrm
 End Function

lovely!

 

Prevent Partially Painted Windows

Sometimes when you display a form only some of the controls appear. After a pause the remaining controls appear. This does not have a professional look. This has become much less apparent in VB 5.0 because of dramatic improvements in screen painting.

example:

 frmPerson.Show vbModeless
 frmPerson.Refresh

The Refresh method will ensure that the form repainting is complete before executing any other code in the routine.