|
-
Dec 30th, 2003, 10:06 PM
#1
Thread Starter
New Member
[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:
Dim trayicon As New System.Windows.Forms.NotifyIcon
Dim icon As New Icon("C:\myicon.ico")
trayicon.Icon = icon
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.
-
Dec 30th, 2003, 10:42 PM
#2
Sleep mode
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
-
Dec 30th, 2003, 10:53 PM
#3
But then he would require a form
-
Dec 30th, 2003, 10:58 PM
#4
Thread Starter
New Member
How do I create a notify icon, WITHOUT a creating a form though?
-
Dec 30th, 2003, 11:02 PM
#5
Originally posted by Safrax
How do I create a notify icon, WITHOUT a creating a form though?
VB Code:
Dim trayicon As New System.Windows.Forms.NotifyIcon
Dim icon As New Icon("C:\myicon.ico")
trayicon.Icon = icon
trayicon.Visible = True
That is the code you originally posted and it doesn't mention a form anywhere. It will work without a form.
-
Dec 30th, 2003, 11:12 PM
#6
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.
-
Dec 30th, 2003, 11:17 PM
#7
Thread Starter
New Member
It's not working here. Are there any references I need? Right now I've got System and System.Windows.Forms.
-
Dec 30th, 2003, 11:33 PM
#8
Thread Starter
New Member
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:
Dim quit As New System.Windows.Forms.MenuItem
trayicon.ContextMenu.MenuItems.Add(quit)
-
Dec 31st, 2003, 12:26 AM
#9
Frenzied Member
This works for me:
VB Code:
Dim trayicon As New System.Windows.Forms.NotifyIcon
Dim icon As New Icon("C:\myicon.ico")
trayicon.Icon = icon
trayicon.Visible = True
Dim mn As New ContextMenu
trayicon.ContextMenu = mn
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
-
Dec 31st, 2003, 12:30 AM
#10
Frenzied Member
And also this:
VB Code:
Dim trayicon As New System.Windows.Forms.NotifyIcon
Dim icon As New Icon("C:\myicon.ico")
trayicon.Icon = icon
trayicon.Visible = True
Dim mn As New ContextMenu
Dim mnitem As New MenuItem("Exit")
mn.MenuItems.Add(mnitem)
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
-
Dec 31st, 2003, 12:53 AM
#11
Thread Starter
New Member
Here's my sub for creating a new tray icon with a context menu! Many thanks to you guys for helping out.
VB Code:
Sub CreateTrayIcon()
'creates a new tray icon
Dim TrayIcon As New System.Windows.Forms.NotifyIcon
'creates a new icon
Dim Icon As New System.Drawing.Icon("C:\icon.ico")
'assigns the icon to the tray icon
TrayIcon.Icon = Icon
'makes the trayicon visible
TrayIcon.Visible = True
'Sets the text on the menu item
mnuQuit.Text = "Quit"
'Creates a new context menu
Dim TrayIconContextMenu As New System.Windows.Forms.ContextMenu
'Adds items to the new context menu
TrayIconContextMenu.MenuItems.Add(mnuQuit)
'Assigns the created contextmenu to the trayicon
TrayIcon.ContextMenu = TrayIconContextMenu
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.
-
Dec 31st, 2003, 01:32 AM
#12
Frenzied Member
add this to previous code I sent:
VB Code:
AddHandler mnitem.Click, AddressOf HandleClick
and
VB Code:
Private Sub HandleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Do something here for example
MessageBox.Show("clicked")
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
-
Dec 31st, 2003, 02:02 AM
#13
Thread Starter
New Member
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.
-
Dec 31st, 2003, 02:49 AM
#14
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:
Module Sub_Main
WithEvents mnuExit As New System.Windows.Forms.MenuItem
Public cntxtMenu As New System.Windows.Forms.ContextMenu
Public TrayIcon As New System.Windows.Forms.NotifyIcon
Sub Main()
TrayIcon.Icon = New System.Drawing.Icon("test.ico")
mnuExit.Text = "Exit"
cntxtMenu.MenuItems.Add(mnuExit)
TrayIcon.ContextMenu = cntxtMenu
TrayIcon.Visible = True
MessageBox.Show("Testing...")
End Sub
Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
End Sub
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.
-
Dec 31st, 2003, 03:10 AM
#15
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.
-
Dec 31st, 2003, 07:57 AM
#16
Sleep mode
Alternatively , you can build windows service since it doen't require a form to run the code inside .
-
Dec 31st, 2003, 11:10 AM
#17
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.
-
Dec 31st, 2003, 01:14 PM
#18
Thread Starter
New Member
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:
Option Strict On
Imports System.Windows.Forms
Module Tray_Icon
WithEvents mnuExit As New System.Windows.Forms.MenuItem
Public cntxtMenu As New System.Windows.Forms.ContextMenu
Public TrayIcon As New System.Windows.Forms.NotifyIcon
Public TrayThread As New System.Threading.Thread(AddressOf CreateTrayIcon)
Public Sub CreateTrayIcon()
TrayIcon.Icon = New System.Drawing.Icon("icon.ico")
mnuExit.Text = "Exit"
cntxtMenu.MenuItems.Add(mnuExit)
TrayIcon.ContextMenu = cntxtMenu
TrayIcon.Visible = True
Do Until False
'to exit the application you will need to set Module1.looping to False
'This thread sleeps for 50 ms, wakes up, does events, then goes back to sleep
Threading.Thread.CurrentThread.Sleep(50)
Application.DoEvents()
Loop
End Sub
Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Shutdown()
End Sub
End Module
This thread is now resolved. Much thanks to everyone that has helped.
-
Dec 31st, 2003, 02:25 PM
#19
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
-
Apr 21st, 2005, 09:18 AM
#20
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|