Form.TopMost Conflicts With MsgBox

D

Don

I've got a form which (in a roundabout manner) calls a second form into
existence which has its TopMost property set (the first form is not the
parent of this second form because of the roundabout manner in which the
second form is created and displayed). The user can then still work on the
first form while the second form floats around above it.

If the first form displays a messagebox via the MsgBox method, the
messagebox appears behing the second, TopMost form and is unreachable. Is
there anyway around this odd behavior? Does this have something to do with
the second form not having the first as its parent?

- Don
 
D

Don

Here's some quick code you can try to reproduce the problem.

- First, create a new, blank project.
- Create two forms, named Form1 and Form2.
- On Form1, place two buttons named Button1 and Button2.
- Post the following code into Form1's code module:


Private WithEvents SecondForm As Form2 = New Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

SecondForm.TopMost = True
SecondForm.Show()

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

MsgBox("test")

End Sub


Press Button1 first to bring up the second form, then press Button2. The
messagebox appears behind the second form. :(

- Don
 
T

tommaso.gastaldi

Hi Don, that behaviour is normal, because of TopMost.

See if something like the following can equally fit your needs:

Private WithEvents SecondForm As Form2 = New Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SecondForm.TopMost = False
SecondForm.Owner = Me
SecondForm.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
MsgBox("test")
End Sub

-tom

Don ha scritto:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top