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
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