How to Automatically Close a Message Box in VB6

How to Automatically Close a Message Box in VB6 thumbnail
Message boxes are used to notify or get a response from the user.

Message boxes are primarily used in VB6 programs to indicate to the user that something requires their attention. A message box might display an error message or it might be asking the user a question, providing a "Yes" and a "No" button on the form. The message box might also be informing the user that something else is about to happen within the program, asking if they'd like to continue. It might benefit the user to leave that message displayed for five seconds and then just automatically move to the next activity within the program. This setting can be done in just a few steps.

Instructions

    • 1

      Open a new Standard EXE Visual Basic project. A form named "Form1" will be created by default. Add a new module to the project by right-clicking on the project name and clicking "Add" and then "module." The default name for the new module will be "Module1."

    • 2

      Rename the form and the module as something meaningful such as "frmMain" for the form and "modTimer" for the module. In the "(General) (Declarations)" area of "modTimer", declare the following Windows functions: KillTimer, FindWindow, SetForegroundWindow. Each of these functions is referenced in the Windows "user32" library.

      Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

      Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

      Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

    • 3

      Declare a constant that will be received to close the message box. The constant declaration is "Public Const NV_CLOSEMSGBOX As Long = &H5000&." Also define a subroutine called "TimerSub" in "modTimer." The declaration for "TimerSub" looks like:

      Public Sub TimerSub(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)

    • 4

      Kill any other timer first within "TimerSub" to make sure that the new timer is the only one active. The function is "KillTimer." Next, check the value of "idEvent" to see if it's equal to "NV_CLOSEMSGBOX." If so, find the message box with "FindWindow" and send the "Enter" key to the message box using "SendKeys." This will force the default button click to execute and thus close the message box.

    • 5

      Test the functionality defined in "TimerSub" by adding a button to "frmMain." In the button click event code, set the timer:

      SetTimer hWnd, NV_CLOSEMSGBOX, 5000, AddressOf TimerProc

      This will force the message box to close in 5000 milliseconds or 5 seconds. Select "Run" and "Start" from the menu in VB to run the program.

Related Searches:

References

  • Photo Credit Photodisc/Photodisc/Getty Images

Comments

Related Ads

Featured