Is it possible to disable context menu items based on the selection on Treeview
I will have some sort of nodes for a treeview as follows
Root |-> some.txt(A text file which was added at runtime) |->Child(child for some.txt) 开发者_如何学Python |-> child1(child for child)
I designed my context menu with some options as New and Remove
What i need is when i righclick on Root, child or child i would like to disable the Remove option
For a ContextMenu, you can handle the ContextMenu.Popup event and enable/disable menu options before the menu is shown.
For a ContextMenuStrip, you can do the same using the Opening event.
For example, if you use the Menu item Tag property to determine if remove is supported (This is just for the example). You can do some thing like this
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if ((int)treeView1.SelectedNode.Tag == 1)
{
removeToolStripMenuItem.Enabled = true;
}
else
{
removeToolStripMenuItem.Enabled = false;
}
}
// Remove all the Empty sub menu items
int counter = MainMenu.Items[1].ChildItems.Count;
for(int i=0; i<counter;i++)
{
foreach (MenuItem item in MainMenu.Items[1].ChildItems)
{
if (item.ChildItems.Count != 0)
continue;
MainMenu.Items[1].ChildItems.Remove(item);
break;
}
}
精彩评论