By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,105 Members | 2,569 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,105 IT Pros & Developers. It's quick & easy.

NotifyIcon ContextMenu Bug

P: n/a
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 --
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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 --

Nov 16 '05 #2

P: n/a
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 --


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.