Form.BringToFront() with NotifyIcon DblClick

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I want my form to be shown when I double click the notify icon. When the notify Icon is DblClicked I show the form.

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] NotifyIcon1_MouseDoubleClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] NotifyIcon1.MouseDoubleClick
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

but it does not come to the front.

i also tried the following code individually and together in both the NotifyIcon DblClick Event and the Form Visible Changed Event.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2][COLOR=#000000].Focus()[/COLOR]
[/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BringToFront()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Refresh()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TopMost = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TopMost = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE]

My form will show itself in front when I am working with just the IDE but whenever I open another window then test the DblClick it never works. I even deployed the project assuming the IDE was controlling the situation and nothing.
 
Kaushik Dey said:
i have 100 power point slides. every slides have x+y
how to write a macro in vb to make x-y in every slides

Kaushik Dey, what does this have to do with giving a form focus?
 
strange problem I have.

I have a strange problem. My form isn't giving me the activate method. I have looked for it with Form1.Activate(). I remember this in 2003 but I can't find it in my project. It's simply not an option. Or maybe I am calling it wrong all together. Can you please supply the command or method please? Sorry to be a pain.
 
*scratch* that's weird. I've had similar issues with stuff like the Closing event which is hidden but still works. I checked and Activate() is there as clear as day.

The code is exactly as you put it Form.Activate() . Just try the code and, unless it generates an error, use it. I dunno, I've written tons of weird code which uses methods that are hidden by VS and it all works fine. What language are you writing in?
 
Are you sure that you're calling it on a form instance and not on the class? The NotifyIcon should be a member of the form, so the DoubleClick event handler should also be a member of the form, so you should just be calling Me.Activate.

As for the Closing event, it has been deprecated so it is not shown. If you used it the IDE would tell you as much. You should be using FormClosing instead.
 
Nope, the IDE just lets the code sit there happily without any notifications whatsoever: Handles Me.Closing (and this is a project created in VS 2005 from scratch). Why in the name of all that is logical and sensible would they bother changing an event name from Closing to FormClosing? What a waste of time.
 
why don't you set your Form's WindowState to minimized on form_load & ShowInTaskBar = False , then you change it's state on DoubleClick of the NotifyIcon.
like this ...
VB.NET:
    Private WithEvents nIcon As NotifyIcon
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyBase.WindowState = FormWindowState.Minimized
        MyBase.ShowInTaskbar = False
        nIcon = New NotifyIcon()
        nIcon.Visible = True
        nIcon.Icon = MyBase.Icon
        nIcon.BalloonTipText = "Double Click the Icon to show the Form"
        nIcon.BalloonTipTitle = MyBase.Text
    End Sub
 
 
    Private Sub nIcon_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles nIcon.MouseDoubleClick
        MyBase.WindowState = FormWindowState.Normal
        MyBase.ShowInTaskbar = True
        MyBase.BringToFront()
    End Sub
:)
 
UncleRonin said:
Why in the name of all that is logical and sensible would they bother changing an event name from Closing to FormClosing? What a waste of time.
There's nothing illogical about it. The two events do not behave the same way. The Closing event still exists so that old applications that are upgraded will still behave the same way. The FormClosing event is the preferred option. If you call Application.Exit then no Closing or Closed events will be raised, but FormClosing and FormClosed will be. I don't know if there are any other differences but that would be enough to bugger some apps if they just changed the behaviour of the Closing and Closed events.

As for the IDE telling you, my apologies for that. I could have sworn that it did but I must have read it in the help rather than in the code editor. Here's a quote from the MSDN2 help topic for Form.Closing:
The Closing event is obsolete in the .NET Framework version 2.0; use the FormClosing event instead.
 
Dynamic, that is exactly the code I have used. Still does not work. And Me.Activate() does cause an error for me. Also, My.Forms.Form1.Activate() causes an error. I am begginning to become curious as to why I do not have this method and everyone else does. I also am useing VB 2005 .NET 2.0. This form is inherited from another form that I am useing but in the previous form I never overrode the Activate method. It should still be intact as usual. However; I also looked at another project that I have that doesn't inherit from another form and Me.Activate() was not available there either. this is odd. I also tried like you asked to use them even though intellisense didn't see it and there was an error.


Beats me.....
 
Fix Here

OK, I APOLOGIZE completely. I was wrong. The Me.Activate() is there and it works great. What was happening was it wouldn't let me type it in. I would type it and press enter and it was filling it with Me.ActivateMdiChildForm() It took me tricking the intellisense and then going back and deleting the extra characters to get it typed in. I am sorry I ran round about this but I wasn't getting it to work before. You guys were correct and Me.Activate() was the answer. I already knew this from before but since I wasn't able to type it in I thought this had changed. Thank you for your help.
 
In VB 2005 the Intellisense listing has two tabs. Activate is not included in the Common tab but it is in the All tab. I'm guessing that you had the Common tab showing. If you select the All tab I think it should be selected by default in future.
 
DUH, I'm a moron..... Yes, when I click all there it is plain as day. I think I deserve a slap in the face. I really ain't that big a noob. Please, someone slap me.
 
Back
Top