Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 494
Developer Avatar asked Sep 18 '10 10:09

Developer


1 Answers

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;
  }
}
like image 83
Chris Taylor Avatar answered Nov 05 '22 13:11

Chris Taylor