I've been working on an application which has a NotifyIcon (system tray
icon), and a corresponding ContextMenu. I want to be able to update this
menu dynamically. However, when I make changes to the menu, it seems to
disappear. This only breaks when the context menu is tied to a NotifyIcon -
not to any other control.
Below is a C# file for a form which should illustrate this (at least it does
on my machine!). The same contextmenu is tied to the NotifyIcon and the
button on the form. Same function is used to update the menu. Note - you
will need an icon included in the project as an Embedded Resource (I called
mine "testIcon.ico").
Anyone have any idea how I can get the menu to behave when tied to the
NotifyIcon?
Thanks,
Derrick
---Form1.cs (sorry for lack of formatting)---
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private NotifyIcon myTrayIcon;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
myTrayIcon = new NotifyIcon();
myTrayIcon.Visible = true;
myTrayIcon.Text = "Cathexis Web Uploader";
myTrayIcon.ContextMenu = this.contextMenu1;
myTrayIcon.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssemb ly().GetManifestResourceSt
ream("WindowsApplication1.testIcon.ico"));
LoadMenuItems("");
}
private void LoadMenuItems(string selectedItemText)
{
this.menuItem1.MenuItems.Clear();
MenuItem miSelected;
//Create the currently selected item
if(selectedItemText == "")
{
miSelected = new MenuItem("(none)");
}
else
{
miSelected = new MenuItem(selectedItemText); //don't need event handler for
this one
miSelected.Checked = true;
}
menuItem1.MenuItems.Add(miSelected);
//Add a seperator
menuItem1.MenuItems.Add(new MenuItem("-"));
//Add the History items
//This would actually come from a seperate class
string[] historyList = GetHistoryItems();
foreach(string s in historyList)
{
if(selectedItemText != s) //weed out duplicates
menuItem1.MenuItems.Add(new MenuItem(s, new
EventHandler(SubMenuItem_Click)));
}
}
private void SubMenuItem_Click(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
LoadMenuItems(mi.Text);
}
private string[] GetHistoryItems()
{
return new string[]{"History1", "History2", "History3"};
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.ContextMenu = this.contextMenu1;
this.button1.Location = new System.Drawing.Point(104, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Test1";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Test2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
---END OF FORM1.cs -- 2 2893
remove menuItem1, clear its menuitems, add new menuitems to menuItem1,
re-add menuItem1.
--
Mick Doherty http://dotnetrix.co.uk/nothing.html
"Derrick" <de***********@hotmail.com> wrote in message
news:Ln*********************@news20.bellglobal.com ... I've been working on an application which has a NotifyIcon (system tray icon), and a corresponding ContextMenu. I want to be able to update this menu dynamically. However, when I make changes to the menu, it seems to disappear. This only breaks when the context menu is tied to a NotifyIcon - not to any other control.
Below is a C# file for a form which should illustrate this (at least it does on my machine!). The same contextmenu is tied to the NotifyIcon and the button on the form. Same function is used to update the menu. Note - you will need an icon included in the project as an Embedded Resource (I called mine "testIcon.ico").
Anyone have any idea how I can get the menu to behave when tied to the NotifyIcon?
Thanks,
Derrick
---Form1.cs (sorry for lack of formatting)---
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private NotifyIcon myTrayIcon;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
// myTrayIcon = new NotifyIcon();
myTrayIcon.Visible = true;
myTrayIcon.Text = "Cathexis Web Uploader";
myTrayIcon.ContextMenu = this.contextMenu1;
myTrayIcon.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssemb ly().GetManifestResourceSt ream("WindowsApplication1.testIcon.ico"));
LoadMenuItems("");
}
private void LoadMenuItems(string selectedItemText)
{
this.menuItem1.MenuItems.Clear();
MenuItem miSelected;
//Create the currently selected item
if(selectedItemText == "")
{
miSelected = new MenuItem("(none)");
}
else
{
miSelected = new MenuItem(selectedItemText); //don't need event handler for this one
miSelected.Checked = true;
}
menuItem1.MenuItems.Add(miSelected);
//Add a seperator
menuItem1.MenuItems.Add(new MenuItem("-"));
//Add the History items
//This would actually come from a seperate class
string[] historyList = GetHistoryItems();
foreach(string s in historyList)
{
if(selectedItemText != s) //weed out duplicates
menuItem1.MenuItems.Add(new MenuItem(s, new EventHandler(SubMenuItem_Click)));
}
}
private void SubMenuItem_Click(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
LoadMenuItems(mi.Text);
}
private string[] GetHistoryItems()
{
return new string[]{"History1", "History2", "History3"};
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.ContextMenu = this.contextMenu1;
this.button1.Location = new System.Drawing.Point(104, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Test1";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Test2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
} ---END OF FORM1.cs --
That sounds like a bit of a hack to me, but it works great! Thanks very
much, Mick!
Derrick
"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:%2****************@tk2msftngp13.phx.gbl... remove menuItem1, clear its menuitems, add new menuitems to menuItem1, re-add menuItem1.
-- Mick Doherty http://dotnetrix.co.uk/nothing.html
"Derrick" <de***********@hotmail.com> wrote in message news:Ln*********************@news20.bellglobal.com ... I've been working on an application which has a NotifyIcon (system tray icon), and a corresponding ContextMenu. I want to be able to update
this menu dynamically. However, when I make changes to the menu, it seems to disappear. This only breaks when the context menu is tied to a NotifyIcon - not to any other control.
Below is a C# file for a form which should illustrate this (at least it does on my machine!). The same contextmenu is tied to the NotifyIcon and the button on the form. Same function is used to update the menu. Note -
you will need an icon included in the project as an Embedded Resource (I called mine "testIcon.ico").
Anyone have any idea how I can get the menu to behave when tied to the NotifyIcon?
Thanks,
Derrick
---Form1.cs (sorry for lack of formatting)---
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private NotifyIcon myTrayIcon;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
// myTrayIcon = new NotifyIcon();
myTrayIcon.Visible = true;
myTrayIcon.Text = "Cathexis Web Uploader";
myTrayIcon.ContextMenu = this.contextMenu1;
myTrayIcon.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssemb ly().GetManifestResourceSt ream("WindowsApplication1.testIcon.ico"));
LoadMenuItems("");
}
private void LoadMenuItems(string selectedItemText)
{
this.menuItem1.MenuItems.Clear();
MenuItem miSelected;
//Create the currently selected item
if(selectedItemText == "")
{
miSelected = new MenuItem("(none)");
}
else
{
miSelected = new MenuItem(selectedItemText); //don't need event handler for this one
miSelected.Checked = true;
}
menuItem1.MenuItems.Add(miSelected);
//Add a seperator
menuItem1.MenuItems.Add(new MenuItem("-"));
//Add the History items
//This would actually come from a seperate class
string[] historyList = GetHistoryItems();
foreach(string s in historyList)
{
if(selectedItemText != s) //weed out duplicates
menuItem1.MenuItems.Add(new MenuItem(s, new EventHandler(SubMenuItem_Click)));
}
}
private void SubMenuItem_Click(object sender, EventArgs e)
{
MenuItem mi = (MenuItem)sender;
LoadMenuItems(mi.Text);
}
private string[] GetHistoryItems()
{
return new string[]{"History1", "History2", "History3"};
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// button1
//
this.button1.ContextMenu = this.contextMenu1;
this.button1.Location = new System.Drawing.Point(104, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{ this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "Test1";
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Test2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
} ---END OF FORM1.cs --
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: petterl |
last post by:
I have tried to find the error in the code below but I always get "
An unhandled exception of type 'System.NullReferenceException' occurred in
Unknown Module.
Additional information: Object...
|
by: Chris Melnick |
last post by:
I am trying to write an application that runs in the system tray, so I am
using a NotifyIcon not associated with a Form. My "main" class has an
instance of the NotifyIcon, and its ContextMenu, as...
|
by: Mike Allen |
last post by:
Hi Everyone,
My problem concerns ContextMenus. It's easy enough to create one and apply
it to a NotifyIcon. When it's compiled and run, it works beautifully. Right
click and there you are. But I...
|
by: Me |
last post by:
I'm getting a NullReferenceException in Unknown Module when I follow
the below steps to create a simple NotifyIcon app that creates the
context menu on the fly(see a little analysis after the...
|
by: Glen |
last post by:
Can anyone tell me if there is a workable method to get the mouse cursor
position on the screen or the NotifyIcon position? I need to display a
context menu for the NotifyIcon when clicked and I'd...
|
by: Claire |
last post by:
Sorry Ive added this twice (sortof) but if I'd added an addendum to the
first one then this would probably have been ignored.
This problem affects a ContextMenu attached to a NotifyIcon object. I...
|
by: mdb |
last post by:
My app has a notify icon in the systray. I want the left mouse click to
initiate a menu, and a right-mouse click to do something else. Normally,
for a button, I would listen to MouseDown and if I...
|
by: nospam |
last post by:
I am using VB Net 2005 and want to use the notifyicon. I do not have a
form, only a class. I have no problems showing or modifying the
NotifyIcon, however, I am having a problem handling the...
|
by: starrysky |
last post by:
I have a program which puts an icon in the notification area and has a menu associated with it available by right clicking on the icon. I want the menu items to be selected by single left clicks but...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |