Dynamic add to context menu only works 1st time??

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

    Dynamic add to context menu only works 1st time??

    I am dynamically appending to a context menu when user right mouse clicks on
    a treeView. The first time I right-mouse click I see the appended menu.
    Subsequently, the menu items I append do not display.

    this.contextMen u1.MenuItems.Ad dRange(new System.Windows. Forms.MenuItem[]
    {this.mnuParent });

    private void treeView1_Mouse Down(object sender,
    System.Windows. Forms.MouseEven tArgs e)
    {
    if( e.Button == MouseButtons.Ri ght )
    {
    mnuParent.MenuI tems.Clear();
    for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
    {
    MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem );

    mnuParent.MenuI tems.Add(mnu);
    }
    contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
    }
    }

    Thanks in advance for any tips or suggestions,

    -Ed Sutton


  • timtos

    #2
    Re: Dynamic add to context menu only works 1st time??

    I think they do display but not every time. ;-)
    Saying that the rest of your code is correct,
    the only problem you have is that you used

    contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));

    and not

    contextMenu1.Sh ow(this.treeVie w1, new System.Drawing. Point(e.X, e.Y));

    With this call you have associated the contextMenu with the form and not with the treeView Control.
    Hope this helps! Greetings,
    timtos.

    "Ed Sutton" <no_s_p_a_m.esu tton@nomadics.c om> wrote in message news:OlGZUnwdDH A.1008@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I am dynamically appending to a context menu when user right mouse clicks on
    > a treeView. The first time I right-mouse click I see the appended menu.
    > Subsequently, the menu items I append do not display.
    >
    > this.contextMen u1.MenuItems.Ad dRange(new System.Windows. Forms.MenuItem[]
    > {this.mnuParent });
    >
    > private void treeView1_Mouse Down(object sender,
    > System.Windows. Forms.MouseEven tArgs e)
    > {
    > if( e.Button == MouseButtons.Ri ght )
    > {
    > mnuParent.MenuI tems.Clear();
    > for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
    > {
    > MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem );
    >
    > mnuParent.MenuI tems.Add(mnu);
    > }
    > contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
    > }
    > }
    >
    > Thanks in advance for any tips or suggestions,
    >
    > -Ed Sutton
    >
    >[/color]


    Comment

    • Ed Sutton

      #3
      Re: Dynamic add to context menu only works 1st time??

      timtos,

      Thank you very much for looking at this.

      I have the same problem after changing the code as you suggested. The menu
      displays every time I right-mosue click, but only the first time displays
      the dynamically created menu.

      contextMenu1.Sh ow(this.treeVie w1, new System.Drawing. Point(e.X, e.Y)).

      I will go beat my head on the wall some more...

      Thank you very much for your suggestion,

      -Ed
      [color=blue]
      > I think they do display but not every time. ;-)
      > Saying that the rest of your code is correct,
      > the only problem you have is that you used
      >
      > contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
      >
      > and not
      >
      > contextMenu1.Sh ow(this.treeVie w1, new System.Drawing. Point(e.X, e.Y));
      >
      > With this call you have associated the contextMenu with the form and
      > not with the treeView Control.
      > Hope this helps! Greetings,
      > timtos.
      >
      > "Ed Sutton" <no_s_p_a_m.esu tton@nomadics.c om> wrote in message
      > news:OlGZUnwdDH A.1008@TK2MSFTN GP12.phx.gbl...[color=green]
      >> I am dynamically appending to a context menu when user right mouse
      >> clicks on
      >> a treeView. The first time I right-mouse click I see the appended
      >> menu.
      >> Subsequently, the menu items I append do not display.
      >>
      >> this.contextMen u1.MenuItems.Ad dRange(new
      >> System.Windows. Forms.MenuItem[] {this.mnuParent });
      >>
      >> private void treeView1_Mouse Down(object sender,
      >> System.Windows. Forms.MouseEven tArgs e)
      >> {
      >> if( e.Button == MouseButtons.Ri ght )
      >> {
      >> mnuParent.MenuI tems.Clear();
      >> for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
      >> {
      >> MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
      >> );
      >>
      >> mnuParent.MenuI tems.Add(mnu);
      >> }
      >> contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
      >> }
      >> }
      >>
      >> Thanks in advance for any tips or suggestions,
      >>
      >> -Ed Sutton[/color][/color]


      Comment

      • timtos

        #4
        Re: Dynamic add to context menu only works 1st time??

        You still have the same problem?
        That is nearly not possible. I´ve tried it out myself now and for me your source is working!
        Have you associated the contextMenu with the treeView?

        Greetings,
        timtos.

        "Ed Sutton" <no_s_p_a_m.esu tton@nomadics.c om> wrote in message news:Oq9ifWxdDH A.2524@TK2MSFTN GP09.phx.gbl...[color=blue]
        > timtos,
        >
        > Thank you very much for looking at this.
        >
        > I have the same problem after changing the code as you suggested. The menu
        > displays every time I right-mosue click, but only the first time displays
        > the dynamically created menu.
        >
        > contextMenu1.Sh ow(this.treeVie w1, new System.Drawing. Point(e.X, e.Y)).
        >
        > I will go beat my head on the wall some more...
        >
        > Thank you very much for your suggestion,
        >
        > -Ed
        >[color=green]
        > > I think they do display but not every time. ;-)
        > > Saying that the rest of your code is correct,
        > > the only problem you have is that you used
        > >
        > > contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
        > >
        > > and not
        > >
        > > contextMenu1.Sh ow(this.treeVie w1, new System.Drawing. Point(e.X, e.Y));
        > >
        > > With this call you have associated the contextMenu with the form and
        > > not with the treeView Control.
        > > Hope this helps! Greetings,
        > > timtos.
        > >
        > > "Ed Sutton" <no_s_p_a_m.esu tton@nomadics.c om> wrote in message
        > > news:OlGZUnwdDH A.1008@TK2MSFTN GP12.phx.gbl...[color=darkred]
        > >> I am dynamically appending to a context menu when user right mouse
        > >> clicks on
        > >> a treeView. The first time I right-mouse click I see the appended
        > >> menu.
        > >> Subsequently, the menu items I append do not display.
        > >>
        > >> this.contextMen u1.MenuItems.Ad dRange(new
        > >> System.Windows. Forms.MenuItem[] {this.mnuParent });
        > >>
        > >> private void treeView1_Mouse Down(object sender,
        > >> System.Windows. Forms.MouseEven tArgs e)
        > >> {
        > >> if( e.Button == MouseButtons.Ri ght )
        > >> {
        > >> mnuParent.MenuI tems.Clear();
        > >> for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
        > >> {
        > >> MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
        > >> );
        > >>
        > >> mnuParent.MenuI tems.Add(mnu);
        > >> }
        > >> contextMenu1.Sh ow(this, new System.Drawing. Point(e.X, e.Y));
        > >> }
        > >> }
        > >>
        > >> Thanks in advance for any tips or suggestions,
        > >>
        > >> -Ed Sutton[/color][/color]
        >
        >[/color]


        Comment

        • Ed Sutton

          #5
          Re: Dynamic add to context menu only works 1st time??

          > You still have the same problem?[color=blue]
          > That is nearly not possible. I´ve tried it out myself now and for me
          > your source is working!
          > Have you associated the contextMenu with the treeView?[/color]

          Not intentionally. I simply drag and dropped a contextMenu on my main form.

          How do you associated a contextMenu a treeView control?

          I pasted all code from my main form class below.

          -Ed

          using System;
          using System.Drawing;
          using System.Collecti ons;
          using System.Componen tModel;
          using System.Windows. Forms;
          using System.Data;
          namespace WindowsApplicat ion3
          {
          /// <summary>
          /// Summary description for Form1.
          /// </summary>
          public class Form1 : System.Windows. Forms.Form
          {
          private System.Windows. Forms.TreeView treeView1;
          private System.Windows. Forms.ContextMe nu contextMenu1;
          private System.Windows. Forms.MenuItem mnuParent;
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.Componen tModel.Containe r components = null;
          public Form1()
          {
          //
          // Required for Windows Form Designer support
          //
          InitializeCompo nent();
          //
          // TODO: Add any constructor code after InitializeCompo nent call
          //
          }
          /// <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.treeView1 = new System.Windows. Forms.TreeView( );
          this.contextMen u1 = new System.Windows. Forms.ContextMe nu();
          this.mnuParent = new System.Windows. Forms.MenuItem( );
          this.SuspendLay out();
          //
          // treeView1
          //
          this.treeView1. ImageIndex = -1;
          this.treeView1. Location = new System.Drawing. Point(48, 32);
          this.treeView1. Name = "treeView1" ;
          this.treeView1. SelectedImageIn dex = -1;
          this.treeView1. Size = new System.Drawing. Size(184, 224);
          this.treeView1. TabIndex = 0;
          this.treeView1. MouseDown += new
          System.Windows. Forms.MouseEven tHandler(this.t reeView1_MouseD own);
          //
          // contextMenu1
          //
          this.contextMen u1.MenuItems.Ad dRange(new System.Windows. Forms.MenuItem[] {
          this.mnuParent} );
          //
          // mnuParent
          //
          this.mnuParent. Index = 0;
          this.mnuParent. Text = "Append Here";
          //
          // Form1
          //
          this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
          this.ClientSize = new System.Drawing. Size(292, 273);
          this.Controls.A ddRange(new System.Windows. Forms.Control[] {
          this.treeView1} );
          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());
          }
          private void treeView1_Mouse Down(object sender,
          System.Windows. Forms.MouseEven tArgs e)
          {
          if( e.Button == MouseButtons.Ri ght )
          {
          System.Diagnost ics.Debug.Write Line("Build menu..");
          mnuParent.MenuI tems.Clear();
          for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
          {
          // Create a list of logger versions to export
          string strMenuText = "Version " + (iNumMenuItem);
          MenuItem mnu = new MenuItem(strMen uText);

          mnuParent.MenuI tems.Add(mnu);
          }
          contextMenu1.Sh ow(treeView1, new System.Drawing. Point(e.X, e.Y));
          }
          }
          }
          }


          Comment

          • timtos

            #6
            Re: Dynamic add to context menu only works 1st time??


            "Ed Sutton" <no_s_p_a_m.esu tton@nomadics.c om> wrote in message news:%234F5Qlxd DHA.3788@tk2msf tngp13.phx.gbl. ..
            [color=blue]
            > How do you associate a contextMenu a treeView control?[/color]

            You can do it with visual studio. Just look for the property ContextMenu
            in the TreeView properties! There is a combobox, where you can select
            your contextMenu.

            Or do it in the code - like this:
            this.treeView.C ontextMenu = this.contextMen u1;

            Hope this finally helps ;-)
            Greetings,
            timtos.


            Comment

            • Ed Sutton

              #7
              Re: Dynamic add to context menu only works 1st time??

              timtos,

              You did it! IT now works perfectly. You are good. :-)

              I will now cease banging my head on the wall.

              Thank you, thank you, , thank you..

              -Ed
              [color=blue]
              > You can do it with visual studio. Just look for the property
              > ContextMenu
              > in the TreeView properties! There is a combobox, where you can select
              > your contextMenu.
              >
              > Or do it in the code - like this:
              > this.treeView.C ontextMenu = this.contextMen u1;
              >
              > Hope this finally helps ;-)
              > Greetings,
              > timtos.[/color]


              Comment

              Working...