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:

  1. Thanks a lot. I like it.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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

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

    ReplyDelete
  7. 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

    ReplyDelete
  8. 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

    ReplyDelete
  9. Thank God i found you, thank you
    God Bless you

    ReplyDelete
  10. thank God i found you, thank you
    God bless you

    ReplyDelete
  11. thank God i found you, thank you
    God bless you

    ReplyDelete
  12. Thanks!it works...!

    ReplyDelete

Note: Only a member of this blog may post a comment.