Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ContextMenu for treeview node
Message
From
30/11/2005 17:50:08
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01071675
Message ID:
01073511
Views:
16
Einar,

I had a great vacation ... it was for my birthday present ... 5 days on the French Riviera!! =)

I usually create context menus by hand rather than with the designer ... six of one, half dozen of the other, IMHO. Enabling/disabling or showing/hiding menu items is basically the way I would have done it too .... so, you done good. <g>

~~Bonnie




>Bonnie,
>Thanks for the reply. What is vacation? <s> Hope you had a nice relaxing time.
>I belive I solved my own problem before turkey-day (I haven't had time working on that project since then). I arrived at a solution designing the context menu using the designer and then just enabling/disabling or showing/hiding menu items as appropriate depending on which node was right-clicked. I do appreciate your code, cuz I learn a lot from it (you code very nice:).
>Do you usually create your context menus by had or do you use the designer?
>
>>Hey Einar,
>>
>>I would have replied earlier, but I've been on vacation. Here's something I wrote up awhile ago that deals with this issue:
>>
>>// Set the Tree's ContextMenu
>>this.oTree.ContextMenu = this.oContextMenu
>>
>>// Set the ContextMenu Popup event handler
>>this.oContextMenu.Popup += new System.EventHandler(this.oContextMenu_Popup);
>>
>>private void oContextMenu_Popup(object sender, System.EventArgs e)
>>{
>>  this.oContextMenu.MenuItems.Clear();
>>  MenuItem oItem;
>>
>>  // Create all the appropriate ContextMenu items. This can vary
>>  // depending on which node in the TreeView was clicked. I have a Node class
>>  // defined in the control that adds additional functionality to the TreeNode class,
>>  // so I can tell which type of Node has been selected
>>  MyTreeNode node = (MyTreeNode)this.oTree.SelectedNode;
>>  if (node.IsTypeOne)
>>  {
>>    // Create one type of context menu
>>    oItem = new MenuItem("Text For Type One");
>>    oItem.Click += new System.EventHandler(this.oMenu_Handler);
>>    this.oContextMenu.MenuItems.Add(oItem);
>>  }
>>  else if (node.IsTypeTwo)
>>  {
>>    // Create another type of context menu
>>    oItem = new MenuItem("Text For Type Two");
>>    oItem.Click += new System.EventHandler(this.oMenu_Handler);
>>    this.oContextMenu.MenuItems.Add(oItem);
>>  }
>>}
>>
>>private void oMenu_Handler(object sender, System.EventArgs e)
>>{
>>  MenuItem oItem = (MenuItem)sender;
>>
>>  // Process whichever ContextMenu item was selected
>>  if (item.Text == "Text For Type One")
>>    // code to process type one
>>  else if (item.Text == "Text For Type Two")
>>    // code to process type two
>>}
>>
>>
>>~~Bonnie
>>
>>
>>
>>>I have a treeview with my own custom treeviewnode class. The treeviewnode class keeps track of several things (like what type the node is). I want to display a different context menu when I right-click on node type #1 than when I right-click node type #2 and for another node type I might not even want the context menu to appear when I right-click.
>>>
>>>I've got a feeling that I need to use the mousedown and mouseup events in conjunction with the GetNodeAt() method to determine what node was right clicked and then display (or not) the context menu based on that.
>>>
>>>Does this seem to be the right approach? Should I create the context menus in the designer or in code? How do I display a context menu from a right-click event (without just adding the contextmenu in the property sheet)?
>>>
>>>This seems like fun.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform