Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Don't stop timers when a MsgBox is active

Total Hit ( 2077)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.