How to Determine Display State of a VB Form, Modal or Modeless

Last reviewed: August 11, 1997
Article ID: Q77316

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

The Show method in the Visual Basic for Windows language can display a form either as modal or modeless. No direct support exists in the language to determine the display state of the form without maintaining global variables that contain the display state of the form. However, the Windows API function GetWindowLong can be used to check the display state of the form.

MORE INFORMATION

When Visual Basic for Windows displays a modal form (.Show 1), all other forms will be modified to contain the Window Style WS_DISABLED. The Windows API function GetWindowLong can be used to return the Window Style of another form to check for the WS_DISABLED style.

The following code demonstrates this process:

Add the following to the General Declarations section of Form1 and Form2:

DefInt A-Z Global Const GWL_STYLE = (-16) Global Const WS_DISABLED = &H8000000

Declare Function GetWindowLong& Lib "user" (ByVal hWnd, ByVal nIndex)

Form1.Frm

Sub Form_Click ()
  ' Flip between "Modeless" and "Modal" display states.
  Static ShowStyle
  Unload form2
  form2.Show ShowStyle
  ShowStyle = (ShowStyle + 1) Mod 2
End Sub

Form2.Frm

Sub Form_Paint ()
  ' Get the Window Style for Form1.
   WinStyle& = GetWindowLong(Form1.hWnd, GWL_STYLE)
   If WinStyle& And WS_DISABLED Then
      ' The WS_DISABLED style is set on "FORM1" when "FORM2"
      ' is displayed with the Modal flag (Show 1).
      Print "Modal    - Show 1"
   Else
      ' The WS_DISABLED style is not set on "FORM1" when "FORM2"
      ' is displayed with the Modeless flag (Show or Show 0).
      Print "Modeless - Show"
   End If
End Sub

Keywords          : APrgWindow PrgOptTips kbcode kbhowto kbtlc
Version           : 1.0 2.0 3.0
Platform          : WINDOWS
Issue type        : kbhowto
Solution Type     : kbcode


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.