|
-
Nov 17th, 2021, 12:22 PM
#1
Thread Starter
Lively Member
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?
-
Nov 17th, 2021, 06:19 PM
#2
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.
-
Nov 18th, 2021, 09:07 AM
#3
Thread Starter
Lively Member
Re: How to ensure that a form is displayed on top?
-
Dec 1st, 2021, 10:17 PM
#4
Banned
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)
-
Dec 1st, 2021, 10:52 PM
#5
Re: How to ensure that a form is displayed on top?
 Originally Posted by Amerigoware
Here's something I used once a long time ago...
Please tell me it was in a galaxy far, far away.
-
Dec 2nd, 2021, 05:01 PM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|