Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Sep 25, 1999

Don't stop timers when a MsgBox is active

Not all developers know that Timer controls in compiled VB5 and VB6 applications aren't stopped by MsgBox statement, so the Timer's Timer event procedure is regularly executed even when the message box is being displayed on the screen.

However, inside interpreted applications under all VB versions, Timers are stopped when a MsgBox is active, which means that, if you rely on the code in the Timer event procedure, you can't completely test and debug your app withing the VB environment.

To do so, you must use the MessageBox API function instead of the built-in MsgBox function. The API function has a similar syntax, but it takes an additional argument that states the parent window of the message box dialog (you can use Me.hWnd), and the order of the Title and Buttons arguments is reversed. Even more interesting, all the VB constants can be used without any problem for the Buttons argument:


Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal _
    hWnd As Long, ByVal lpText As String, ByVal lpCaption As String, _
    ByVal wType As Long) As Long
To test this function, create a new form and place one timer, one label, and two command buttons on its surface, then type this code:

Private Sub Command1_Click()
    MsgBox "The Timer STOPS!", vbOKOnly, "Regular MsgBox"
End Sub

Private Sub Command2_Click()
    MessageBox Me.hWnd, "The Timer DOESN'T STOP", "MessageBox API function", _
        vbOKOnly + vbExclamation
End Sub

Private Sub Timer1_Timer()
Label1.Caption = Time
End Sub
Even better, you can wrap the API function into a MsgBox-like function that takes the same arguments in the same order:

Function MsgBox2(Prompt As String, Optional Buttons As VbMsgBoxStyle, _
    Optional Title As Variant) As VbMsgBoxResult
    If IsMissing(Title) Then Title = App.Title
    MsgBox2 = MessageBox(Screen.ActiveForm.hWnd, Prompt, Title, Buttons)
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date