views:

59

answers:

2

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

+2  A: 

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;
  }
}
Chris Taylor
But for selectednode.tag i am always getting Null value
Dorababu
Hey i set the tag property but i am unable to disable the toolstripmenu
Dorababu
@Dorababu, is the node selected in the tree? I tested this and it does work. Can you share some of your code? Did you set a break point in the event handler and check that the event is being fired?
Chris Taylor
Thanks for the code it works for me. I just use Convert.ToInt16
Dorababu
A: 

Hi chris,

         My code is as follows but i am getting an error as Invalidcast exception was handled

  private void contextMenu_Opening(object sender, CancelEventArgs e)
     {
            if ((int)tvwACH.SelectedNode.Tag ==1)
            {
                Remove.Enabled = false;
            }
            if ((int)tvwACH.SelectedNode.Tag == 3)
            {
                Remove.Enabled = false;
            }
            if ((int)tvwACH.SelectedNode.Tag == 3)
            {
                Remove.Enabled = true;
            }
   }
Dorababu