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

  • Thread starter Thread starter Ed Sutton
  • Start date Start date
E

Ed Sutton

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.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem );

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton
 
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.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, 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.
 
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.Show(this.treeView1, 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
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.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, 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 said:
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.contextMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton
 
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 said:
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.Show(this.treeView1, 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
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.Show(this, new System.Drawing.Point(e.X, e.Y));

and not

contextMenu1.Show(this.treeView1, 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 said:
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.contextMenu1.MenuItems.AddRange(new
System.Windows.Forms.MenuItem[] {this.mnuParent});

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
MenuItem mnu = new MenuItem("Test Menu " + iNumMenuItem
);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(this, new System.Drawing.Point(e.X, e.Y));
}
}

Thanks in advance for any tips or suggestions,

-Ed Sutton
 
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?

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem mnuParent;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <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.treeView1 = new System.Windows.Forms.TreeView();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.mnuParent = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(48, 32);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(184, 224);
this.treeView1.TabIndex = 0;
this.treeView1.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuParent});
//
// mnuParent
//
this.mnuParent.Index = 0;
this.mnuParent.Text = "Append Here";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeView1});
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());
}
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Right )
{
System.Diagnostics.Debug.WriteLine("Build menu..");
mnuParent.MenuItems.Clear();
for( int iNumMenuItem = 0; iNumMenuItem < 9; iNumMenuItem++)
{
// Create a list of logger versions to export
string strMenuText = "Version " + (iNumMenuItem);
MenuItem mnu = new MenuItem(strMenuText);

mnuParent.MenuItems.Add(mnu);
}
contextMenu1.Show(treeView1, new System.Drawing.Point(e.X, e.Y));
}
}
}
}
 
Ed Sutton said:
How do you associate a contextMenu a treeView control?

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.ContextMenu = this.contextMenu1;

Hope this finally helps ;-)
Greetings,
timtos.
 
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
 
Back
Top