Msgbox Causes VB App to Lose Focus

I have a VB6 app that uses msgboxes to provide information to the user like "Process Complete".  For some users, these msgboxes cause the VB app to lose focus and they have to alt-tab back.  Is there a way to stop this from happening?
LVL 1
tlchavetAsked:
Who is Participating?
 
List244Connect With a Mentor Commented:
I am a little confused by your question, but perhaps this is what you want?

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

MessageBox 0, "MessageBox Caption", "MessageBox Title", vbInformation
Form1.SetFocus

This will allow you to create message box's that do not freeze your application. However, it will freeze
any events in the same function, until the message box has finished. The above code would then
pop up a message box, when they click okay, the form will get focus. Though any other code that
is normally running during that time will not pause with the messagebox like it would with the
normal MsgBox function.
0
 
edwardiiiConnect With a Mentor Commented:
Hi, tlchavet.

Can you post the exact code before and after the MsgBox code line that causes focus to move away from the VB App?
I'm wondering if the following would enforce return to the VB app?

    MsgBox "howdy"
    Form1.SetFocus
0
 
tlchavetAuthor Commented:
The code before and after varies and not every user experiences the problem.  Will a form1.setfocus trigger any events other than form_setfocus?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
tlchavetAuthor Commented:
To be sure I am understanding both responses, whether I use the standard msgbox or the function MessageBox, to ensure the focus stays in my app I should use the form1.setfocus?
0
 
List244Commented:
Setting focus just brings focus back to the application. Actually the msgbox itself should be doing that, yet
even doing a better job at it. Clicking the okay button on the msgbox should force your application back
in front of other windows too, which the setting focus will NOT.

The point of using MessageBox is just to allow your application to continue running. Instead of freezing
like the normal command button does.
0
 
List244Commented:
The reason I do set focus with MessageBox is because I am giving it a false Hwnd, so it is not attached to my program.
That means that when you click okay it will not return to the program. Setting focus will force it to do that. I do not
know that setfocus will help in the case of "msgbox" but you can try it and see. Truthfully, I do not understand why the
msgbox you have currently is not returning focus to the application upon pressing okay.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.