blitzzy
asked on
Show form without losing focus on active window
I have an application written in VB.NET 2005 that has a main form with a datagridview containing information that is updated every 10 seconds and pops up a new form when certain thresholds have been met. This new, borderless form is like an alert that is displayed near the user's system tray.
My problem is that when this alert form is displayed, the form gets the main focus from whatever the user is doing at the time. For example, if the user is typing an e-mail in Outlook, the focus is transferred from Outlook to the popup.
How can I get my popup alert to be displayed as the TopMost form without interrupting the user? For instance, when a new e-mail is received, a popup is displayed in MSN Messenger but it doesn't take focus away from what the user is doing yet it still stays on top of all the forms.
I have tried overriding the ShowWithoutActivation property in .NET 2.0 but it doesn't seem to work.
My problem is that when this alert form is displayed, the form gets the main focus from whatever the user is doing at the time. For example, if the user is typing an e-mail in Outlook, the focus is transferred from Outlook to the popup.
How can I get my popup alert to be displayed as the TopMost form without interrupting the user? For instance, when a new e-mail is received, a popup is displayed in MSN Messenger but it doesn't take focus away from what the user is doing yet it still stays on top of all the forms.
I have tried overriding the ShowWithoutActivation property in .NET 2.0 but it doesn't seem to work.
Protected Overrides ReadOnly Property ShowWithoutActivation() As Boolean
' Gets a value indicating whether the window will be activated when shown
Get
' Set to TRUE so the popup won't interrupt the user's work by taking
' input focus away from the user's current window
Return True
End Get
End Property
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is using a Windows API...the 8 is the value of SW_SHOWNA.
http://msdn.microsoft.com/en-us/library/ms633548.aspx
I've never tried doing it the .Net way before...it seemed to work fine for me.
Can you show me more code where you create the form and display it?
http://msdn.microsoft.com/en-us/library/ms633548.aspx
I've never tried doing it the .Net way before...it seemed to work fine for me.
Can you show me more code where you create the form and display it?
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f2 As New Form2
f2.Show()
End Sub
End Class
Public Class Form2
Protected Overrides ReadOnly Property ShowWithoutActivation() As Boolean
Get
Return True
End Get
End Property
End Class
ASKER
Idle_Mind:
Sorry for the delay in a reply to your last message. I've been down the past few days trying to get things back to normal on my new PC at work.
Here is the code that you requested where I create the form and display it. Since there can potentially be many instances of the same form (but with different contents for the labels), I have utliized an array.
Sorry for the delay in a reply to your last message. I've been down the past few days trying to get things back to normal on my new PC at work.
Here is the code that you requested where I create the form and display it. Since there can potentially be many instances of the same form (but with different contents for the labels), I have utliized an array.
' Global Variables - Alert Popup
Const m_MaxAlertForm As Integer = 500
Private AlertPopUp(m_MaxAlertForm) As QueueAlertPopUp
Private Sub CheckThreshold()
' Code for checking thresholds
' Show popup alert
ShowAlertPopup(AlertFormNum, AlertLevel.SevereLevel, AlertMessage)
End Sub
Private Sub ShowAlertPopup(ByVal AlertFormNum As Integer, ByVal aLevel As Integer, ByVal AlertMessage As String)
Dim PopupLocation As Point
' Calculate the popup location and display
PopupLocation = CalcPopupPlacement()
' Display popup at specified location
AlertPopUp(AlertFormNum) = New QueueAlertPopUp(aLevel, AlertMessage)
AlertPopUp(AlertFormNum).Location = PopupLocation
AlertPopUp(AlertFormNum).Show()
End Sub
ASKER
Using the sample code, I was able to modify my code to get the feature to work. Thanks for the help!
Hi blitzzy,
Going thru my inbox trying to re-visit questions that need following up on...
Have you made any progress on this one?
If not, which method are you currently using?
...the ShowWindow() API method
...the Overrides ShowWithoutActivation() method
Both methods worked for me. =\
Going thru my inbox trying to re-visit questions that need following up on...
Have you made any progress on this one?
If not, which method are you currently using?
...the ShowWindow() API method
...the Overrides ShowWithoutActivation() method
Both methods worked for me. =\
ASKER
I am using the Overrides ShowWithoutActivation() method. I don't know why, but sometimes it works fine (the focus isn't removed from whatever the user is doing) and other times it takes focus away. So frustrating! I just want it to act like the notifications you get in Outlook or MSN Messenger when the user receives a new e-mail, etc.
Even if I have to update a label on the popup form, as long as I use the Overrides method, shouldn't this still work?
Even if I have to update a label on the popup form, as long as I use the Overrides method, shouldn't this still work?
I can't get it to fail for me... =\
I'm even updating a label and the title on the "popup" forms with a Timer and it still doesn't steal focus.
Are there any controls on the popup that can get focus?
I'm even updating a label and the title on the "popup" forms with a Timer and it still doesn't steal focus.
Are there any controls on the popup that can get focus?
ASKER
The popup contains labels (updated) and a PictureBox (may be updated). It's borderless but may be moved by detecting the MouseDown and MouseUp events.
ASKER
I tried your method and the form does not remove focus from whatever the user is doing. However, the labels and an image that I have on the popup are not being displayed. My main form passes values to the alert so when the popup form is loaded, the text labels are written accordingly.