Problems with dynamically adding menu items to ContextMenu

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm running into a problem that I can't figure out and would love any help I can get. I'm writing a C# app using Windows Forms and I'm getting some wacky behavior.

I have a Tree Control on a simple form. I created a ContextMenu (at design time) and I manually display it when the user right clicks on any node. In this Context Menu, I dynamically add submenu items to an existing menu item ("New")depending on which node is selected. The first time that the "New" Sub menu is displayed, all items are present as I would expect. The next time you right click on the same node, the new menu items never show up. I set a breakpoint after the function call to add the items and the number of items in the collection is right.

I wrote a dummy app as an example and have included it below. To show the bug, right click on the "Wilma" node first and see that the "New" menu item has 3 additional items in a submenu. If you click off of the context menu and right click on "Wilma" again, you won't see the 3 items in the "New" submenu. If you look at the "Delete" item, the submenu items are added at design time and they always work correctly.

I've included code snippets below (hopefully I won't go over a message size limit).

public Form1()
{
InitializeComponent();

int index = treeView.Nodes.Add( new TreeNode( "Flintstones" ) );
treeView.Nodes[ index ].Nodes.Add( "Fred" );
treeView.Nodes[ index ].Nodes.Add( "Wilma" );
treeView.Nodes[ index ].Nodes.Add( "Barney" );
treeView.Nodes[ index ].Nodes.Add( "Betty" );
treeView.ExpandAll();
}

private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// remove all items in the "New" list. they will be re-added below
newItem.MenuItems.Clear();

Point pt = new Point( e.X, e.Y );
TreeNode node = treeView.GetNodeAt( pt );

// if we found node where the user clicked
if ( node != null )
{
treeView.SelectedNode = node;

// special case Wilma for "New" items
if ( node.Text == "Wilma" )
{
for (int i=0 ;i<3; i++)
{
string itemText = String.Format( "Item{0}", i + 1 );
MenuItem addedItem = new MenuItem( itemText );
addedItem.Enabled = true;
newItem.MenuItems.Add( addedItem );
}
}
else
{
MenuItem addedItem = new MenuItem( "Empty" );

newItem.MenuItems.Add( addedItem );

addedItem.Enabled = false;
}

myContextMenu.Show( this, pt );
}
}

Thanks.

Rich
 
Try the contextMenu Popup Event. That is what I use for these things.

contextMenutreeViewProduct.Popup += new
System.EventHandler(this.contextMenu_Popup);

private void contextMenu_Popup(object sender, System.EventArgs e)
{
System.Windows.Forms.ContextMenu t_contextMenu =
(System.Windows.Forms.ContextMenu) sender;
// This is only needed if you support more that one ContextMenu in this
Popup
if (t_contextMenu == contextMenutreeViewProduct)
{
TreeNode t_TreeNode = treeViewProduct.SelectedNode;
if (t_TreeNode != null) // Tree may be empty
{

//--------------------------------------------------------------------------
--
// Turn off all the Menus, we will only activat thos that are needed

//--------------------------------------------------------------------------
--
// .Visible is not supported b< NET Framework.Compact, more the pity.
menuItemtreeViewProduct_Copy.Enabled = false;
menuItemtreeViewProduct_Delete.Enabled = false;

//--------------------------------------------------------------------------
--
// Through the ImageIndex we know how the Branch string is formatted
// - that is why Note12.ico is used three times in imageListMainFrame
(8,9,10)

//--------------------------------------------------------------------------
--
if ((t_TreeNode.SelectedImageIndex == 1) ||
(t_TreeNode.SelectedImageIndex == 2)) // DataBase
{ // Database
// Only the DataBase Menu should be shown
menuItemtreeViewProduct_Copy.Enabled = true;
}
if ( t_TreeNode.Text == "Wilma" )
{
for (int i=0 ;i<3; i++)
{
string itemText = String.Format( "Item{0}", i + 1 );
MenuItem addedItem = new MenuItem( itemText );
addedItem.Enabled = true;
newItem.MenuItems.Add( addedItem );
}
}
}
}

Maby the OnMouseUp event is not being fired up.
Hope this helps.

Mark Johnson, Berlin Germany
(e-mail address removed)





Rich Moore said:
I'm running into a problem that I can't figure out and would love any help
I can get. I'm writing a C# app using Windows Forms and I'm getting some
wacky behavior.
I have a Tree Control on a simple form. I created a ContextMenu (at
design time) and I manually display it when the user right clicks on any
node. In this Context Menu, I dynamically add submenu items to an existing
menu item ("New")depending on which node is selected. The first time that
the "New" Sub menu is displayed, all items are present as I would expect.
The next time you right click on the same node, the new menu items never
show up. I set a breakpoint after the function call to add the items and
the number of items in the collection is right.
I wrote a dummy app as an example and have included it below. To show the
bug, right click on the "Wilma" node first and see that the "New" menu item
has 3 additional items in a submenu. If you click off of the context menu
and right click on "Wilma" again, you won't see the 3 items in the "New"
submenu. If you look at the "Delete" item, the submenu items are added at
design time and they always work correctly.
I've included code snippets below (hopefully I won't go over a message size limit).

public Form1()
{
InitializeComponent();

int index = treeView.Nodes.Add( new TreeNode( "Flintstones" ) );
treeView.Nodes[ index ].Nodes.Add( "Fred" );
treeView.Nodes[ index ].Nodes.Add( "Wilma" );
treeView.Nodes[ index ].Nodes.Add( "Barney" );
treeView.Nodes[ index ].Nodes.Add( "Betty" );
treeView.ExpandAll();
}

private void treeView1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
 
Rich: Was having similar problems the other night - just wanted to give you
some other thoughts to chew on.

http://support.microsoft.com/default.aspx?scid=kb;EN-US;811399


My problem had to do with the fact that i was using the MouseUp event which
was firing after the built in ContextMenu processing of the WinForm control
so it was screwing things up. I also noticed that you're not checking which
button was used on the mouse up - might want to add that.

Good Luck!



Rich Moore said:
I'm running into a problem that I can't figure out and would love any help
I can get. I'm writing a C# app using Windows Forms and I'm getting some
wacky behavior.
I have a Tree Control on a simple form. I created a ContextMenu (at
design time) and I manually display it when the user right clicks on any
node. In this Context Menu, I dynamically add submenu items to an existing
menu item ("New")depending on which node is selected. The first time that
the "New" Sub menu is displayed, all items are present as I would expect.
The next time you right click on the same node, the new menu items never
show up. I set a breakpoint after the function call to add the items and
the number of items in the collection is right.
I wrote a dummy app as an example and have included it below. To show the
bug, right click on the "Wilma" node first and see that the "New" menu item
has 3 additional items in a submenu. If you click off of the context menu
and right click on "Wilma" again, you won't see the 3 items in the "New"
submenu. If you look at the "Delete" item, the submenu items are added at
design time and they always work correctly.
I've included code snippets below (hopefully I won't go over a message size limit).

public Form1()
{
InitializeComponent();

int index = treeView.Nodes.Add( new TreeNode( "Flintstones" ) );
treeView.Nodes[ index ].Nodes.Add( "Fred" );
treeView.Nodes[ index ].Nodes.Add( "Wilma" );
treeView.Nodes[ index ].Nodes.Add( "Barney" );
treeView.Nodes[ index ].Nodes.Add( "Betty" );
treeView.ExpandAll();
}

private void treeView1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
 
Back
Top