Results 1 to 6 of 6

Thread: How to ensure that a form is displayed on top?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    How to ensure that a form is displayed on top?

    I have a vb program that reacts to an event (a signal from a modem) by displaying a form. I have the following (simplified) code:

    Code:
    Public Class MainForm
            Dim eventOccurred As Boolean = False
    
    	Sub TestLoad() Handles Me.Load
    	    ....
    	    WaitEvent()
    	End Sub
    	
    	Sub WaitEvent()
    	    Do
    	        Thread.Sleep(500) 'Checks every 500 ms to see if eventOccurred has been set to True (by another subroutine).
    		Application.DoEvents()
    		If eventOccurred then
    		    ...
    		    Me.WindowState = System.Windows.Forms.FormWindowState.Normal
    		    Me.TopMost = True
    		    Me.Show()
    		    eventOccurred = False
    		End If
    	    Loop
    	End Sub
    End Class
    Sometimes when the event occurs the form is displayed as desired. Other times, though, it will not display but rather appears minimized in the system tray and can't be made to show. (If there's a pattern as to when it displays or doesn't display, I haven't figured it out.)

    How can I ensure that the form will always be displayed in a topmost window?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to ensure that a form is displayed on top?

    That code is utterly terrible and you need to ditch it immediately. Calling Application.DoEvents is rarely a good solution to anything and calling it in a loop is never a good solution to anything. You should follow the CodeBank link in my signature and check out my thread on creating a Formless Tray Application. That will demonstrate how to run a WinForms app without displaying a form at all. You can then monitor whatever it is you want to monitor in your ApplicationContext and then just display the form normally when this event occurs. You would then set the TopMost property of the form in the designer.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Re: How to ensure that a form is displayed on top?

    Thank you.

  4. #4
    Banned
    Join Date
    Apr 2018
    Location
    https://t.me/pump_upp
    Posts
    79

    Re: How to ensure that a form is displayed on top?

    Here's something I used once a long time ago...

    Code:
        Public Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As IntPtr) As IntPtr
        End Function
    Code:
    If eventOccurred then
    		    ...
    		    Me.WindowState = System.Windows.Forms.FormWindowState.Normal
    		    Me.TopMost = True
            SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to ensure that a form is displayed on top?

    Quote Originally Posted by Amerigoware View Post
    Here's something I used once a long time ago...
    Please tell me it was in a galaxy far, far away.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2012
    Posts
    75

    Re: How to ensure that a form is displayed on top?

    Thanks, Amerigoware. I've added your suggestion to my collection of code snippets in case I run into a similar problem. I discovered later in the case I asked about that the problem had nothing to do with the code. I had two different programs that called this one and it turns out one of them was calling it with the properties set to minimized, so depending upon which one called it, the form would either display or not. I've corrected that and now all seems to be well.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width