Results 1 to 20 of 20

Thread: [RESOLVED] Notify Icon with right click menu.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13

    [RESOLVED] Notify Icon with right click menu.

    I need to make a program that does not have any forms in it whatsoever, but does put a notify icon with a right click menu in the system tray. I've tried doing something like this:

    VB Code:
    1. Dim trayicon As New System.Windows.Forms.NotifyIcon
    2.             Dim icon As New Icon("C:\myicon.ico")
    3.             trayicon.Icon = icon
    4.             trayicon.Visible = True

    But that doesn't work.

    Even if I could create a notify icon I don't know how to add menu items to it that do anything.

    If someone could post a sample of some code on how to do either of these things or point me to some place with an example, I'd appreciate it very much.
    Last edited by Safrax; Dec 31st, 2003 at 01:16 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Example - I'm not sure if it has a menu - but you can add a 'Context Menu' then just attach it to the notifyIcon component in the properties window .
    http://www.vbforums.com/showthread.p...hreadid=254796

  3. #3
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Pirate
    Example - I'm not sure if it has a menu - but you can add a 'Context Menu' then just attach it to the notifyIcon component in the properties window .
    http://www.vbforums.com/showthread.p...hreadid=254796
    But then he would require a form

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    Originally posted by Pirate
    Example - I'm not sure if it has a menu - but you can add a 'Context Menu' then just attach it to the notifyIcon component in the properties window .
    http://www.vbforums.com/showthread.p...hreadid=254796
    How do I create a notify icon, WITHOUT a creating a form though?

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Safrax
    How do I create a notify icon, WITHOUT a creating a form though?
    VB Code:
    1. Dim trayicon As New System.Windows.Forms.NotifyIcon
    2.             Dim icon As New Icon("C:\myicon.ico")
    3.             trayicon.Icon = icon
    4.             trayicon.Visible = True

    That is the code you originally posted and it doesn't mention a form anywhere. It will work without a form.

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Edneeis
    That is the code you originally posted and it doesn't mention a form anywhere. It will work without a form.
    No, you didn't listen to what he wanted. He wants it to stay open the entire time (that code won't) and he wants a context menu with it, without creating a form.

    I tried making it with code real quick and, apparently, when I create a context menu and add a menuitem to it, the contextmenu remains empty....***

    My code also uses 17MB of ram and 100% of the CPU, lol. I'll figure something out later when I can fire VS up (I wasn't using the IDE) if no one else does.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    It's not working here. Are there any references I need? Right now I've got System and System.Windows.Forms.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    Ok I've got the tray icon working by adding System.Drawing to the references. But whenever I try to add menuitems to it I get this nasty error:

    Code:
    An unhandled exception of type 'System.NullReferenceException' occurred in Foolirc.exe 
      
     Additional information: Object reference not set to an instance of an object.
    This is what I am doing to add menuitems:

    VB Code:
    1. Dim quit As New System.Windows.Forms.MenuItem
    2. trayicon.ContextMenu.MenuItems.Add(quit)

  9. #9
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    This works for me:
    VB Code:
    1. Dim trayicon As New System.Windows.Forms.NotifyIcon
    2. Dim icon As New Icon("C:\myicon.ico")
    3. trayicon.Icon = icon
    4. trayicon.Visible = True
    5. Dim mn As New ContextMenu
    6. trayicon.ContextMenu = mn
    7. mn.MenuItems.Add("Close")
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    And also this:
    VB Code:
    1. Dim trayicon As New System.Windows.Forms.NotifyIcon
    2. Dim icon As New Icon("C:\myicon.ico")
    3. trayicon.Icon = icon
    4. trayicon.Visible = True
    5. Dim mn As New ContextMenu
    6. Dim mnitem As New MenuItem("Exit")
    7. mn.MenuItems.Add(mnitem)
    8. trayicon.ContextMenu = mn
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  11. #11

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    Here's my sub for creating a new tray icon with a context menu! Many thanks to you guys for helping out.

    VB Code:
    1. Sub CreateTrayIcon()
    2.         'creates a new tray icon
    3.         Dim TrayIcon As New System.Windows.Forms.NotifyIcon
    4.  
    5.         'creates a new icon
    6.         Dim Icon As New System.Drawing.Icon("C:\icon.ico")
    7.  
    8.         'assigns the icon to the tray icon
    9.         TrayIcon.Icon = Icon
    10.  
    11.         'makes the trayicon visible
    12.         TrayIcon.Visible = True
    13.  
    14.         'Sets the text on the menu item
    15.         mnuQuit.Text = "Quit"
    16.  
    17.         'Creates a new context menu
    18.         Dim TrayIconContextMenu As New System.Windows.Forms.ContextMenu
    19.  
    20.         'Adds items to the new context menu
    21.         TrayIconContextMenu.MenuItems.Add(mnuQuit)
    22.  
    23.         'Assigns the created contextmenu to the trayicon
    24.         TrayIcon.ContextMenu = TrayIconContextMenu
    25.     End Sub

    Now I'm left with one last problem. My context menu is totally unresponsive to being clicked when the program is running. I've tried putting it in a thread, but that was just pointless as the icon disappears when the thread stops running.

  12. #12
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    add this to previous code I sent:
    VB Code:
    1. AddHandler mnitem.Click, AddressOf HandleClick
    and
    VB Code:
    1. Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.     ' Do something here for example
    3.     MessageBox.Show("clicked")
    4. End Sub
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    What I meant to say was my context menu is NOT appearing at all when I click on the icon. Rightclicking or left clicking on the icon has no effect. The icon just sits there like a knot on a log. But thanks for answering what would have been my next question Lunatic3.

  14. #14
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    http://msdn.microsoft.com/library/de...ncomponent.asp

    I went there, I created code just like that for C# and VB.NET. Both did not work for some reason, the context menu was unresponcive. After some wondering, I changed the locations of the definitions and now they work!!

    I guess you CANNOT define NotifyIcon and ContextMenu within the Sub using them.

    Both seem to work, VB..NET version has events added onto one menu already.

    VB.NET:
    VB Code:
    1. Module Sub_Main
    2.     WithEvents mnuExit As New System.Windows.Forms.MenuItem
    3.     Public cntxtMenu As New System.Windows.Forms.ContextMenu
    4.     Public TrayIcon As New System.Windows.Forms.NotifyIcon
    5.     Sub Main()
    6.         TrayIcon.Icon = New System.Drawing.Icon("test.ico")
    7.         mnuExit.Text = "Exit"
    8.         cntxtMenu.MenuItems.Add(mnuExit)
    9.         TrayIcon.ContextMenu = cntxtMenu
    10.         TrayIcon.Visible = True
    11.         MessageBox.Show("Testing...")
    12.     End Sub
    13.     Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
    14.  
    15.     End Sub
    16. End Module

    C#:
    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace NotifyIcon
    {
    	public class main
    	{
    		public System.Windows.Forms.ContextMenu cnuMenu = new System.Windows.Forms.ContextMenu();
    		public System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
    		static void Main()
    		{
    			main MaIn = new main();
    			MaIn.CreateMenuStructure();
    			System.Windows.Forms.MessageBox.Show("Testing.....");
    		}
    		private void CreateMenuStructure()
    		{
    			System.Windows.Forms.MenuItem mnuExit = new System.Windows.Forms.MenuItem("Exit");
    			nIcon.Icon = new System.Drawing.Icon("test.ico");
    			cnuMenu.MenuItems.Add(mnuExit);
    			nIcon.ContextMenu = cnuMenu;
    			nIcon.Visible = true;
    			mnuExit.Visible = true;
    		}
    	}
    }
    Last edited by Kasracer; Dec 31st, 2003 at 02:53 AM.

  15. #15
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    I just took a look at Safrax's project and I tried a few different things as well. It seems like the icon will ONLY work if some sort of Form is being used. A messageBox,, a hidden form, as long as there is SOME sort of form it seems to work. Otherwise it remains unresponcive.

  16. #16
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Alternatively , you can build windows service since it doen't require a form to run the code inside .

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The problem is not where the NotifyIcon object is declared or whether or not to show a form but the fact that the NotifyIcon alone doesn't hold a thread open for input. The sub main goes through and executes all of the code and then ends. It doesn't respond to clicks because the only time you see it is while other code is being executed which blocks its responsiveness. Showing a form or MessageBox will hold the thread while awaiting input. You may want to see this thread: http://www.vbforums.com/showthread.p...hreadid=268696 or ask LITHIA what he did to solve this problem.
    Last edited by Edneeis; Dec 31st, 2003 at 02:51 PM.

  18. #18

    Thread Starter
    New Member
    Join Date
    Dec 2003
    Posts
    13
    Edneeis, that thread did the trick. Thank you for point it out.

    Here's the finished module in case anyone ever needs something like this again:

    *Note: A TrayThread.Start() is needed somewhere else in the program to get this to work.

    VB Code:
    1. Option Strict On
    2. Imports System.Windows.Forms
    3.  
    4.  
    5. Module Tray_Icon
    6.  
    7.     WithEvents mnuExit As New System.Windows.Forms.MenuItem
    8.     Public cntxtMenu As New System.Windows.Forms.ContextMenu
    9.     Public TrayIcon As New System.Windows.Forms.NotifyIcon
    10.     Public TrayThread As New System.Threading.Thread(AddressOf CreateTrayIcon)
    11.  
    12.  
    13.     Public Sub CreateTrayIcon()
    14.         TrayIcon.Icon = New System.Drawing.Icon("icon.ico")
    15.         mnuExit.Text = "Exit"
    16.         cntxtMenu.MenuItems.Add(mnuExit)
    17.         TrayIcon.ContextMenu = cntxtMenu
    18.         TrayIcon.Visible = True
    19.  
    20.         Do Until False
    21.             'to exit the application you will need to set Module1.looping to False
    22.  
    23.             'This thread sleeps for 50 ms, wakes up, does events, then goes back to sleep
    24.             Threading.Thread.CurrentThread.Sleep(50)
    25.             Application.DoEvents()
    26.         Loop
    27.     End Sub
    28.  
    29.     Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
    30.         Shutdown()
    31.     End Sub
    32. End Module

    This thread is now resolved. Much thanks to everyone that has helped.

  19. #19
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Edneeis
    The problem is not where the NotifyIcon object is declared or whether or not to show a form but the fact that the NotifyIcon along doesn't hold a thread open for input. The sub main goes through and executes all of the code and then ends. It doesn't respond to clicks because the only time you see it is while other code is being executed which blocks its responsiveness. Showing a form or MessageBox will hold the thread while awaiting input. You may want to see this thread: http://www.vbforums.com/showthread.p...hreadid=268696 or ask LITHIA what he did to solve this problem.
    Bah, figures I was wrong.

    EDIT: I still think Safrax should do away with his Do While, and Do Until fetishes

  20. #20
    Addicted Member Codehammer's Avatar
    Join Date
    Aug 2004
    Posts
    164

    Re: [RESOLVED] Notify Icon with right click menu.

    in Vb a Text box even change will give you access to the Event that you can Actually do this:

    if e.keycode =keys.enter then ....

    But, for a notify Icon, the event only has e.cancel.

    How Would you get the Context menu to show, Only If the Shift key is pressed while you click the notify icon. I tried putting a Sub with that samillar handler as the textbox & it did nothing
    Curiosity SKILLED the cat
    Google Talk from your Mobile phone

    Chat from your mobile or get an emulator like J2ME Wireless Toolkit 2.2

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