NotifyIcon ContextMenu Bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derrick

    NotifyIcon ContextMenu Bug

    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.Collecti ons;

    using System.Componen tModel;

    using System.Windows. Forms;

    using System.Data;

    namespace WindowsApplicat ion1

    {

    /// <summary>

    /// Summary description for Form1.

    /// </summary>

    public class Form1 : System.Windows. Forms.Form

    {

    private System.Windows. Forms.Button button1;

    private System.Windows. Forms.ContextMe nu contextMenu1;

    private System.Windows. Forms.MenuItem menuItem1;

    private System.Windows. Forms.MenuItem menuItem2;

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.Componen tModel.Containe r components = null;

    private NotifyIcon myTrayIcon;

    public Form1()

    {

    //

    // Required for Windows Form Designer support

    //

    InitializeCompo nent();

    //

    // TODO: Add any constructor code after InitializeCompo nent call

    //



    myTrayIcon = new NotifyIcon();

    myTrayIcon.Visi ble = true;

    myTrayIcon.Text = "Cathexis Web Uploader";

    myTrayIcon.Cont extMenu = this.contextMen u1;

    myTrayIcon.Icon = new
    Icon(System.Ref lection.Assembl y.GetExecutingA ssembly().GetMa nifestResourceS t
    ream("WindowsAp plication1.test Icon.ico"));

    LoadMenuItems(" ");

    }

    private void LoadMenuItems(s tring selectedItemTex t)

    {

    this.menuItem1. MenuItems.Clear ();


    MenuItem miSelected;

    //Create the currently selected item

    if(selectedItem Text == "")

    {

    miSelected = new MenuItem("(none )");

    }

    else

    {

    miSelected = new MenuItem(select edItemText); //don't need event handler for
    this one

    miSelected.Chec ked = true;

    }


    menuItem1.MenuI tems.Add(miSele cted);

    //Add a seperator

    menuItem1.MenuI tems.Add(new MenuItem("-"));

    //Add the History items

    //This would actually come from a seperate class

    string[] historyList = GetHistoryItems ();

    foreach(string s in historyList)

    {

    if(selectedItem Text != s) //weed out duplicates

    menuItem1.MenuI tems.Add(new MenuItem(s, new
    EventHandler(Su bMenuItem_Click )));

    }

    }

    private void SubMenuItem_Cli ck(object sender, EventArgs e)

    {

    MenuItem mi = (MenuItem)sende r;

    LoadMenuItems(m i.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.Disp ose();

    }

    }

    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 InitializeCompo nent()

    {

    this.button1 = new System.Windows. Forms.Button();

    this.contextMen u1 = new System.Windows. Forms.ContextMe nu();

    this.menuItem1 = new System.Windows. Forms.MenuItem( );

    this.menuItem2 = new System.Windows. Forms.MenuItem( );

    this.SuspendLay out();

    //

    // button1

    //

    this.button1.Co ntextMenu = this.contextMen u1;

    this.button1.Lo cation = new System.Drawing. Point(104, 112);

    this.button1.Na me = "button1";

    this.button1.Ta bIndex = 0;

    this.button1.Te xt = "button1";

    //

    // contextMenu1

    //

    this.contextMen u1.MenuItems.Ad dRange(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.AutoScaleB aseSize = new System.Drawing. Size(5, 13);

    this.ClientSize = new System.Drawing. Size(292, 273);

    this.Controls.A dd(this.button1 );

    this.Name = "Form1";

    this.Text = "Form1";

    this.ResumeLayo ut(false);

    }

    #endregion

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    Application.Run (new Form1());

    }

    }

    }



    ---END OF FORM1.cs --


  • Mick Doherty

    #2
    Re: NotifyIcon ContextMenu Bug

    remove menuItem1, clear its menuitems, add new menuitems to menuItem1,
    re-add menuItem1.

    --
    Mick Doherty
    Dotnetrix offers Nothing. Standard and Premium versions available.



    "Derrick" <derrick_dunne@ hotmail.com> wrote in message
    news:LnSHd.5461 1$W33.1438258@n ews20.bellgloba l.com...[color=blue]
    > 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.Collecti ons;
    >
    > using System.Componen tModel;
    >
    > using System.Windows. Forms;
    >
    > using System.Data;
    >
    > namespace WindowsApplicat ion1
    >
    > {
    >
    > /// <summary>
    >
    > /// Summary description for Form1.
    >
    > /// </summary>
    >
    > public class Form1 : System.Windows. Forms.Form
    >
    > {
    >
    > private System.Windows. Forms.Button button1;
    >
    > private System.Windows. Forms.ContextMe nu contextMenu1;
    >
    > private System.Windows. Forms.MenuItem menuItem1;
    >
    > private System.Windows. Forms.MenuItem menuItem2;
    >
    > /// <summary>
    >
    > /// Required designer variable.
    >
    > /// </summary>
    >
    > private System.Componen tModel.Containe r components = null;
    >
    > private NotifyIcon myTrayIcon;
    >
    > public Form1()
    >
    > {
    >
    > //
    >
    > // Required for Windows Form Designer support
    >
    > //
    >
    > InitializeCompo nent();
    >
    > //
    >
    > // TODO: Add any constructor code after InitializeCompo nent call
    >
    > //
    >
    >
    >
    > myTrayIcon = new NotifyIcon();
    >
    > myTrayIcon.Visi ble = true;
    >
    > myTrayIcon.Text = "Cathexis Web Uploader";
    >
    > myTrayIcon.Cont extMenu = this.contextMen u1;
    >
    > myTrayIcon.Icon = new
    > Icon(System.Ref lection.Assembl y.GetExecutingA ssembly().GetMa nifestResourceS t
    > ream("WindowsAp plication1.test Icon.ico"));
    >
    > LoadMenuItems(" ");
    >
    > }
    >
    > private void LoadMenuItems(s tring selectedItemTex t)
    >
    > {
    >
    > this.menuItem1. MenuItems.Clear ();
    >
    >
    > MenuItem miSelected;
    >
    > //Create the currently selected item
    >
    > if(selectedItem Text == "")
    >
    > {
    >
    > miSelected = new MenuItem("(none )");
    >
    > }
    >
    > else
    >
    > {
    >
    > miSelected = new MenuItem(select edItemText); //don't need event handler
    > for
    > this one
    >
    > miSelected.Chec ked = true;
    >
    > }
    >
    >
    > menuItem1.MenuI tems.Add(miSele cted);
    >
    > //Add a seperator
    >
    > menuItem1.MenuI tems.Add(new MenuItem("-"));
    >
    > //Add the History items
    >
    > //This would actually come from a seperate class
    >
    > string[] historyList = GetHistoryItems ();
    >
    > foreach(string s in historyList)
    >
    > {
    >
    > if(selectedItem Text != s) //weed out duplicates
    >
    > menuItem1.MenuI tems.Add(new MenuItem(s, new
    > EventHandler(Su bMenuItem_Click )));
    >
    > }
    >
    > }
    >
    > private void SubMenuItem_Cli ck(object sender, EventArgs e)
    >
    > {
    >
    > MenuItem mi = (MenuItem)sende r;
    >
    > LoadMenuItems(m i.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.Disp ose();
    >
    > }
    >
    > }
    >
    > 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 InitializeCompo nent()
    >
    > {
    >
    > this.button1 = new System.Windows. Forms.Button();
    >
    > this.contextMen u1 = new System.Windows. Forms.ContextMe nu();
    >
    > this.menuItem1 = new System.Windows. Forms.MenuItem( );
    >
    > this.menuItem2 = new System.Windows. Forms.MenuItem( );
    >
    > this.SuspendLay out();
    >
    > //
    >
    > // button1
    >
    > //
    >
    > this.button1.Co ntextMenu = this.contextMen u1;
    >
    > this.button1.Lo cation = new System.Drawing. Point(104, 112);
    >
    > this.button1.Na me = "button1";
    >
    > this.button1.Ta bIndex = 0;
    >
    > this.button1.Te xt = "button1";
    >
    > //
    >
    > // contextMenu1
    >
    > //
    >
    > this.contextMen u1.MenuItems.Ad dRange(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.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
    >
    > this.ClientSize = new System.Drawing. Size(292, 273);
    >
    > this.Controls.A dd(this.button1 );
    >
    > this.Name = "Form1";
    >
    > this.Text = "Form1";
    >
    > this.ResumeLayo ut(false);
    >
    > }
    >
    > #endregion
    >
    > /// <summary>
    >
    > /// The main entry point for the application.
    >
    > /// </summary>
    >
    > [STAThread]
    >
    > static void Main()
    >
    > {
    >
    > Application.Run (new Form1());
    >
    > }
    >
    > }
    >
    > }
    >
    >
    >
    > ---END OF FORM1.cs --
    >
    >[/color]


    Comment

    • Derrick

      #3
      Re: NotifyIcon ContextMenu Bug

      That sounds like a bit of a hack to me, but it works great! Thanks very
      much, Mick!

      Derrick

      "Mick Doherty"
      <EXCHANGE#WITH@ AND.REMOVE.SQUA REBRACKETS.[mdaudi100#ntlwo rld.com]> wrote in
      message news:%23qgVzly$ EHA.1396@tk2msf tngp13.phx.gbl. ..[color=blue]
      > remove menuItem1, clear its menuitems, add new menuitems to menuItem1,
      > re-add menuItem1.
      >
      > --
      > Mick Doherty
      > http://dotnetrix.co.uk/nothing.html
      >
      >
      > "Derrick" <derrick_dunne@ hotmail.com> wrote in message
      > news:LnSHd.5461 1$W33.1438258@n ews20.bellgloba l.com...[color=green]
      > > 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[/color][/color]
      this[color=blue][color=green]
      > > 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 -[/color][/color]
      you[color=blue][color=green]
      > > 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.Collecti ons;
      > >
      > > using System.Componen tModel;
      > >
      > > using System.Windows. Forms;
      > >
      > > using System.Data;
      > >
      > > namespace WindowsApplicat ion1
      > >
      > > {
      > >
      > > /// <summary>
      > >
      > > /// Summary description for Form1.
      > >
      > > /// </summary>
      > >
      > > public class Form1 : System.Windows. Forms.Form
      > >
      > > {
      > >
      > > private System.Windows. Forms.Button button1;
      > >
      > > private System.Windows. Forms.ContextMe nu contextMenu1;
      > >
      > > private System.Windows. Forms.MenuItem menuItem1;
      > >
      > > private System.Windows. Forms.MenuItem menuItem2;
      > >
      > > /// <summary>
      > >
      > > /// Required designer variable.
      > >
      > > /// </summary>
      > >
      > > private System.Componen tModel.Containe r components = null;
      > >
      > > private NotifyIcon myTrayIcon;
      > >
      > > public Form1()
      > >
      > > {
      > >
      > > //
      > >
      > > // Required for Windows Form Designer support
      > >
      > > //
      > >
      > > InitializeCompo nent();
      > >
      > > //
      > >
      > > // TODO: Add any constructor code after InitializeCompo nent call
      > >
      > > //
      > >
      > >
      > >
      > > myTrayIcon = new NotifyIcon();
      > >
      > > myTrayIcon.Visi ble = true;
      > >
      > > myTrayIcon.Text = "Cathexis Web Uploader";
      > >
      > > myTrayIcon.Cont extMenu = this.contextMen u1;
      > >
      > > myTrayIcon.Icon = new
      > >[/color][/color]
      Icon(System.Ref lection.Assembl y.GetExecutingA ssembly().GetMa nifestResourceS t[color=blue][color=green]
      > > ream("WindowsAp plication1.test Icon.ico"));
      > >
      > > LoadMenuItems(" ");
      > >
      > > }
      > >
      > > private void LoadMenuItems(s tring selectedItemTex t)
      > >
      > > {
      > >
      > > this.menuItem1. MenuItems.Clear ();
      > >
      > >
      > > MenuItem miSelected;
      > >
      > > //Create the currently selected item
      > >
      > > if(selectedItem Text == "")
      > >
      > > {
      > >
      > > miSelected = new MenuItem("(none )");
      > >
      > > }
      > >
      > > else
      > >
      > > {
      > >
      > > miSelected = new MenuItem(select edItemText); //don't need event handler
      > > for
      > > this one
      > >
      > > miSelected.Chec ked = true;
      > >
      > > }
      > >
      > >
      > > menuItem1.MenuI tems.Add(miSele cted);
      > >
      > > //Add a seperator
      > >
      > > menuItem1.MenuI tems.Add(new MenuItem("-"));
      > >
      > > //Add the History items
      > >
      > > //This would actually come from a seperate class
      > >
      > > string[] historyList = GetHistoryItems ();
      > >
      > > foreach(string s in historyList)
      > >
      > > {
      > >
      > > if(selectedItem Text != s) //weed out duplicates
      > >
      > > menuItem1.MenuI tems.Add(new MenuItem(s, new
      > > EventHandler(Su bMenuItem_Click )));
      > >
      > > }
      > >
      > > }
      > >
      > > private void SubMenuItem_Cli ck(object sender, EventArgs e)
      > >
      > > {
      > >
      > > MenuItem mi = (MenuItem)sende r;
      > >
      > > LoadMenuItems(m i.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.Disp ose();
      > >
      > > }
      > >
      > > }
      > >
      > > 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 InitializeCompo nent()
      > >
      > > {
      > >
      > > this.button1 = new System.Windows. Forms.Button();
      > >
      > > this.contextMen u1 = new System.Windows. Forms.ContextMe nu();
      > >
      > > this.menuItem1 = new System.Windows. Forms.MenuItem( );
      > >
      > > this.menuItem2 = new System.Windows. Forms.MenuItem( );
      > >
      > > this.SuspendLay out();
      > >
      > > //
      > >
      > > // button1
      > >
      > > //
      > >
      > > this.button1.Co ntextMenu = this.contextMen u1;
      > >
      > > this.button1.Lo cation = new System.Drawing. Point(104, 112);
      > >
      > > this.button1.Na me = "button1";
      > >
      > > this.button1.Ta bIndex = 0;
      > >
      > > this.button1.Te xt = "button1";
      > >
      > > //
      > >
      > > // contextMenu1
      > >
      > > //
      > >
      > > this.contextMen u1.MenuItems.Ad dRange(new System.Windows. Forms.MenuItem[][/color][/color]
      {[color=blue][color=green]
      > >
      > > this.menuItem1,
      > >
      > > this.menuItem2} );
      > >
      > > //
      > >
      > > // menuItem1
      > >
      > > //
      > >
      > > this.menuItem1. Index = 0;
      > >
      > > this.menuItem1. Text = "Test1";
      > >
      > > //
      > >
      > > // menuItem2
      > >
      > > //
      > >
      > > this.menuItem2. Index = 1;
      > >
      > > this.menuItem2. Text = "Test2";
      > >
      > > //
      > >
      > > // Form1
      > >
      > > //
      > >
      > > this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
      > >
      > > this.ClientSize = new System.Drawing. Size(292, 273);
      > >
      > > this.Controls.A dd(this.button1 );
      > >
      > > this.Name = "Form1";
      > >
      > > this.Text = "Form1";
      > >
      > > this.ResumeLayo ut(false);
      > >
      > > }
      > >
      > > #endregion
      > >
      > > /// <summary>
      > >
      > > /// The main entry point for the application.
      > >
      > > /// </summary>
      > >
      > > [STAThread]
      > >
      > > static void Main()
      > >
      > > {
      > >
      > > Application.Run (new Form1());
      > >
      > > }
      > >
      > > }
      > >
      > > }
      > >
      > >
      > >
      > > ---END OF FORM1.cs --
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...