Home All Groups Group Topic Archive Search About

Overcoming a MsgBox's shyness

Author
13 Jun 2009 6:52 AM
Howard Kaikow
I am working on an app in which, after all too long, I realized that the app
appeared to be hanging because a Msgbox that was likely hiding behind a Form
that uses SetWindowPos to keep the Form on top.

Looking at the constants for MsgBox, is vbMsgBoxSetForeground the right way
to handle this?
Or will the Form still hide the Msgbox?

Author
13 Jun 2009 8:22 AM
Bill McCarthy
Hi Howard,

It depends on what value is used with SetWindowPos.  If the form is set as
topmost, then yes I'm pretty sure (untested) that it can be in front of a
message box from another form.

Show quoteHide quote
"Howard Kaikow" <kai***@standards.com> wrote in message
news:%23KiT$N$6JHA.5932@TK2MSFTNGP03.phx.gbl...
>I am working on an app in which, after all too long, I realized that the
>app
> appeared to be hanging because a Msgbox that was likely hiding behind a
> Form
> that uses SetWindowPos to keep the Form on top.
>
> Looking at the constants for MsgBox, is vbMsgBoxSetForeground the right
> way
> to handle this?
> Or will the Form still hide the Msgbox?
>
>
Are all your drivers up to date? click for free checkup

Author
13 Jun 2009 8:52 AM
Howard Kaikow
"Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
news:eY$ikAA7JHA.4376@TK2MSFTNGP06.phx.gbl...
> Hi Howard,
>
> It depends on what value is used with SetWindowPos.  If the form is set as
> topmost, then yes I'm pretty sure (untested) that it can be in front of a
> message box from another form.

In this case, it's more complex.

The VB 6 program is dynamically creating an Excel workbook, and creating the
code modules i nthe Excel workbook. one of which is the Workbook_BeforeClose
event.

I finally figured out that when the VB 6 program causes Excel to CLOSE the
workbook just created, the code is hanging in the Workbook_BeforeClose
event.

After creation, the Excel workbook code works as expected. Problem only
occurs when VB 6 is first creating the workbok via Excel.

Fortunately, the MsgBox was only used to test the events, so I could just
delete the Msgbox, but I then noticed the vbMsgBoxSetForeground constant.

Testing that constant in a particular circumstance does not answer the
general question.
I asked here to get the experience of others.
Author
13 Jun 2009 10:50 AM
David Youngblood
Show quote Hide quote
"Howard Kaikow" <kai***@standards.com> wrote...
>
> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote...
>> Hi Howard,
>>
>> It depends on what value is used with SetWindowPos.  If the form is set
>> as
>> topmost, then yes I'm pretty sure (untested) that it can be in front of a
>> message box from another form.
>
> In this case, it's more complex.
>
> The VB 6 program is dynamically creating an Excel workbook, and creating
> the
> code modules i nthe Excel workbook. one of which is the
> Workbook_BeforeClose
> event.
>
> I finally figured out that when the VB 6 program causes Excel to CLOSE the
> workbook just created, the code is hanging in the Workbook_BeforeClose
> event.
>
> After creation, the Excel workbook code works as expected. Problem only
> occurs when VB 6 is first creating the workbok via Excel.
>
> Fortunately, the MsgBox was only used to test the events, so I could just
> delete the Msgbox, but I then noticed the vbMsgBoxSetForeground constant.
>
> Testing that constant in a particular circumstance does not answer the
> general question.
> I asked here to get the experience of others.

As long as you're testing you might also test vbSystemModal. It's not system
modal as described in help, but in my limited experience, it does bring the
msgbox to the top of all applications. Similar to setting topmost.

David
Author
13 Jun 2009 3:29 PM
Howard Kaikow
"David Youngblood" <d**@flash.net> wrote in message
news:uSNRATB7JHA.1764@TK2MSFTNGP06.phx.gbl...
>
> "Howard Kaikow" <kai***@standards.com> wrote...
> As long as you're testing you might also test vbSystemModal. It's not
system
> modal as described in help, but in my limited experience, it does bring
the
> msgbox to the top of all applications. Similar to setting topmost.

I would rarely desire to have all other applications suspended.

If vbMsgBoxSetForeground does the deed, then the question becomes, why not
always use vbMsgBoxSetForeground?
Author
13 Jun 2009 3:43 PM
Howard Kaikow
"Howard Kaikow" <kai***@standards.com> wrote in message
news:uZmkfuD7JHA.5780@TK2MSFTNGP04.phx.gbl...
> I would rarely desire to have all other applications suspended.
>
> If vbMsgBoxSetForeground does the deed, then the question becomes, why not
> always use vbMsgBoxSetForeground?

vbMsgBoxSetForeground  does not do the deed, the VB 6 Form remains on top.
I guess that the workaround would be to use another Form, instead of a
MsgBox, and hope that it goes on top of the main Form.
Author
13 Jun 2009 3:54 PM
Howard Kaikow
Show quote Hide quote
"Howard Kaikow" <kai***@standards.com> wrote in message
news:uZmkfuD7JHA.5780@TK2MSFTNGP04.phx.gbl...
> "David Youngblood" <d**@flash.net> wrote in message
> news:uSNRATB7JHA.1764@TK2MSFTNGP06.phx.gbl...
> >
> > "Howard Kaikow" <kai***@standards.com> wrote...
> > As long as you're testing you might also test vbSystemModal. It's not
> system
> > modal as described in help, but in my limited experience, it does bring
> the
> > msgbox to the top of all applications. Similar to setting topmost.
>
> I would rarely desire to have all other applications suspended.
>
> If vbMsgBoxSetForeground does the deed, then the question becomes, why not
> always use vbMsgBoxSetForeground?

I just saw a note  in the VB & VBA in a Nutshell book.

On multitasking OS, vbSystemModal only has the effect of being on top.
So vbSystemModal does do the deed,

It would have been nice if MSFT had mentioned this in the Help for MsgBox.
Author
13 Jun 2009 10:35 PM
David Youngblood
"Howard Kaikow" <kai***@standards.com> wrote in message >
> I just saw a note  in the VB & VBA in a Nutshell book.
>
> On multitasking OS, vbSystemModal only has the effect of being on top.
> So vbSystemModal does do the deed,
>
> It would have been nice if MSFT had mentioned this in the Help for MsgBox.

In 16 bit windows I beleive it was system modal. This was changed with 32
bit windows, but the vb help was not updated.

David
Author
13 Jun 2009 3:47 PM
Nobody
"Howard Kaikow" <kai***@standards.com> wrote in message
news:%23KiT$N$6JHA.5932@TK2MSFTNGP03.phx.gbl...
>I am working on an app in which, after all too long, I realized that the
>app
> appeared to be hanging because a Msgbox that was likely hiding behind a
> Form
> that uses SetWindowPos to keep the Form on top.
>
> Looking at the constants for MsgBox, is vbMsgBoxSetForeground the right
> way
> to handle this?
> Or will the Form still hide the Msgbox?

This may or may not work: Use MessageBox() API function, and set the hWnd to
the owner form. MsgBox() doesn't have that option.
Author
13 Jun 2009 3:56 PM
Howard Kaikow
"Nobody" <nob***@nobody.com> wrote in message
news:ubdmF5D7JHA.4376@TK2MSFTNGP06.phx.gbl...
> This may or may not work: Use MessageBox() API function, and set the hWnd
to
> the owner form. MsgBox() doesn't have that option.

That likely would work, but I now see that vbSystemModal does the deed.

On a multitasking OS, why would one not want to use vbSystemModal ALL the
time?
Author
13 Jun 2009 4:03 PM
Nobody
"Howard Kaikow" <kai***@standards.com> wrote in message
news:%23s5C99D7JHA.5780@TK2MSFTNGP04.phx.gbl...
> "Nobody" <nob***@nobody.com> wrote in message
> news:ubdmF5D7JHA.4376@TK2MSFTNGP06.phx.gbl...
>> This may or may not work: Use MessageBox() API function, and set the hWnd
> to
>> the owner form. MsgBox() doesn't have that option.
>
> That likely would work, but I now see that vbSystemModal does the deed.
>
> On a multitasking OS, why would one not want to use vbSystemModal ALL the
> time?

Because if the user was working on app1, and app2 decided to show a message,
the user's work is not interrupted, and app2 window and taskbar button
flashes.
Author
13 Jun 2009 4:29 PM
Howard Kaikow
"Nobody" <nob***@nobody.com> wrote in message
news:OflsNCE7JHA.1196@TK2MSFTNGP03.phx.gbl...
> Because if the user was working on app1, and app2 decided to show a
message,
> the user's work is not interrupted, and app2 window and taskbar button
> flashes.
In this case, the prog is automatically run right after the install of a
DLL.
If the user wishes, the program could be run anytime after the install.

But, in this case, I was using the Msgbox only to test the
Workbook_BeforeClose event, so this particular MsgBox will not be in the
final code.

Perhaps, I should use the MessageBox API whenever I use a topmost Form?
Author
13 Jun 2009 4:37 PM
Nobody
"Howard Kaikow" <kai***@standards.com> wrote in message
news:e3LmFQE7JHA.1420@TK2MSFTNGP04.phx.gbl...
> Perhaps, I should use the MessageBox API whenever I use a topmost Form?

I don't know, however, if you need to use MessageBox, make a wrapper for it
to make things easier. Here is an example:

Public Sub ShowMsgBox(Prompt As String, _
    Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, _
    Optional ByVal Title As String = "")

    ' Add code here

End Sub

So all you need is to change MsgBox to ShowMsgBox. I used "VbMsgBoxStyle" so
button names would show in intellisense.
Author
13 Jun 2009 5:13 PM
Howard Kaikow
Show quote Hide quote
"Nobody" <nob***@nobody.com> wrote in message
news:OpmAWVE7JHA.1432@TK2MSFTNGP02.phx.gbl...
> "Howard Kaikow" <kai***@standards.com> wrote in message
> news:e3LmFQE7JHA.1420@TK2MSFTNGP04.phx.gbl...
> > Perhaps, I should use the MessageBox API whenever I use a topmost Form?
>
> I don't know, however, if you need to use MessageBox, make a wrapper for
it
> to make things easier. Here is an example:
>
> Public Sub ShowMsgBox(Prompt As String, _
>     Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, _
>     Optional ByVal Title As String = "")
>
>     ' Add code here
>
> End Sub
>
> So all you need is to change MsgBox to ShowMsgBox. I used "VbMsgBoxStyle"
so
> button names would show in intellisense.

I've got code using MessageBox and MessageBoxEx. I'd use a wrapper if I
decided to oft use the critter.

Option Explicit
    Private Const HWND_TOPMOST = -1
    Private Const SWP_NOACTIVATE = &H10
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_SHOWWINDOW = &H40

    Private Const MB_DEFBUTTON1 = &H0&
    Private Const MB_DEFBUTTON2 = &H100&
    Private Const MB_DEFBUTTON3 = &H200&
    Private Const MB_ICONASTERISK = &H40&
    Private Const MB_ICONEXCLAMATION = &H30&
    Private Const MB_ICONHAND = &H10&
    Private Const MB_ICONINFORMATION = MB_ICONASTERISK
    Private Const MB_ICONQUESTION = &H20&
    Private Const MB_IConstOP = MB_ICONHAND
    Private Const MB_OK = &H0&
    Private Const MB_OKCANCEL = &H1&
    Private Const MB_YESNO = &H4&
    Private Const MB_YESNOCANCEL = &H3&
    Private Const MB_ABORTRETRYIGNORE = &H2&
    Private Const MB_RETRYCANCEL = &H5&

    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
    Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal
hWndInsertAfter As Long, _
        ByVal x As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As
Long, ByVal wFlags As Long)

Private Sub btnExit_Click()
    Unload Me
End Sub

Private Sub btnRunMe_Click()
    MessageBox Me.hWnd, "At the tone the time WAS" & vbCrLf & vbCrLf & Now,
App.Title, MB_OK
End Sub

Private Sub Form_Activate()
    MakeFormTopmost Me
End Sub

Private Sub MakeFormTopmost(frm As Form)
    SetWindowPos frm.hWnd, HWND_TOPMOST, 0, 0, 0, 0, _
        SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
Author
13 Jun 2009 5:26 PM
Howard Kaikow
In the case I questioned, the MessageBox and MessageBoxEx  APIs won't help
because the message is being issued by Excel and it has no idea that VB 6 is
even running. So, in this case, I'd have to use vbSystemModal.

Bookmark and Share