Thursday, May 8, 2008

Keeping a VB.NET Form Always on Top

In VB6, you used a Win32API call to SetWindowPos to get a form to always stay on top. This has been simplified somewhat in VB.NET. Put the following in Form_Load or other appropriate place:

Me.TopMost = True

12 comments:

Anonymous said...

Thanks a lot. I like it.

Anonymous said...

Is there any way to make a window on top of a full screen app? I know this doesn't make too much sense since the app I am working is a "window", but... basically I want my app to sit on top of another app.

srego said...

THis seems to be a common problem if you search around on the internet. Here are some of the articles I found on it:

http://osdir.com/ml/windows.devel.dotnet.winforms/2004-06/msg00369.html

http://www.vbforums.com/showthread.php?p=3249342

http://www.codeproject.com/KB/cs/FullScreenDotNetApp.aspx

http://www.vbforums.com/showthread.php?referrerid=61394&t=525997


I don't think there is really a solution for you in any of these, but it might point you in the right direction.

Anonymous said...

Srego - Thank you for these posts. I followed the link to:
http://www.vbforums.com/showthread.php?p=3249342

The solution there totally did the trick for me. Use a timer, to keep setting the form property to
form1.topmost = true.

I set my timer to every 1 second, and it worked like a charm for me.

Here is the code snipet:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.TopMost = True
End Sub

Anonymous said...

Thank you Srego - the link to
http://www.vbforums.com/showthread.php?p=3249342
totally did the trick for me.

The answer is to use a timer, to keep setting the form topmost = true.

Here is the code snipet. Thanks so much to Srego for posting this info.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.TopMost = True
End Sub

Anonymous said...

i think using of timer every secont to tik may kill my CPU usage and resource

Anonymous said...

It might, but if you add a timer control instead, and double clicks the control to generate the handler code, then set the interval to something more sensible. like every 250 ticks, then that may reduce the impact on your processor usage

Anonymous said...

Take a timer control, set its interval property to 1 then paste the following.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Focus()
End Sub

Yosua.Jimstimoti7 said...

Thank God i found you, thank you
God Bless you

Yosua.Jimstimoti7 said...

thank God i found you, thank you
God bless you

Anonymous said...

thank God i found you, thank you
God bless you

Anonymous said...

Thanks!it works...!