I have a form that has to be on top for a period of time, and then can be set behind other windows normally. Is there anything in addition to setting Me.TopMost
to True
or False
that needs to be done? I ask because it doesn't seem to be working.
Asked
Active
Viewed 4,805 times
2
-
In what way does it seem to not work? – Fredrik Mörk Nov 11 '09 at 15:39
1 Answers
4
It should present no problem. The following code (C#, sorry for that, no VB.NET environment available where I am right now) sets TopMost
to true
, waits for 5 seconds and then toggles TopMost
back to false
.
private void MakeMeTopmostForAWhile()
{
this.TopMost = true;
ThreadPool.QueueUserWorkItem(state =>
{
Thread.Sleep(5000);
this.Invoke((Action)delegate { this.TopMost = false; });
});
}
Note that this does not affect the Z-order of the window immediately; when TopMost
is set to false
, the window will still be on top of other windows. If the window is on top of another window that is also topmost, it will move so that the other topmost window is not covered, but it will remain on top of other non-topmost windows.
Update
Here is the above code in VB.NET (auto-converted, not tested):
Private Sub MakeMeTopmostForAWhile()
Me.TopMost = True
ThreadPool.QueueUserWorkItem(Function(state) Do
Thread.Sleep(5000)
Me.Invoke(DirectCast(Function() Do
Me.TopMost = False
End Function, Action))
End Function)
End Sub

Fredrik Mörk
- 155,851
- 29
- 291
- 343
-
When setting TopMost to true does it immediately affect the Z-order? Meaning should it go straight to the top? – Shawn Nov 11 '09 at 16:08
-
@ShawN: it should at least move in front of any non-topmost windows. I would not guess that it would automatically move in front of other topmost windows. If you want to force that you could call `Me.BringToFront()`. – Fredrik Mörk Nov 11 '09 at 16:10
-
1@Shawn: I did a quick test and it appears as if setting `TopMost = True` forces the window to the front, also in front of other windows that are already topmost. Setting it to false seems to move it back enough to not cover any topmost windows. – Fredrik Mörk Nov 11 '09 at 16:15