Learn how to a build a cloud-first strategyRegister Now


Msgbox Causes VB App to Lose Focus

Posted on 2005-04-29
Medium Priority
Last Modified: 2013-12-25
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?
Question by:tlchavet
  • 3
  • 2
LVL 10

Assisted Solution

edwardiii earned 300 total points
ID: 13896548
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"

Author Comment

ID: 13896558
The code before and after varies and not every user experiences the problem.  Will a form1.setfocus trigger any events other than form_setfocus?

Accepted Solution

List244 earned 700 total points
ID: 13896653
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

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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13896695
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?

Expert Comment

ID: 13896730
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.

Expert Comment

ID: 13896752
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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question