Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Treeview + CheckBox + ContextMenu
Message
 
To
14/02/2004 16:45:09
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00877414
Message ID:
00877429
Views:
12
I got it!

I have set the ContextMenu property of the treeview to ContextMenu I built, and I have added this event handler (the Popup event like you told me):
    Private Sub mnuTreeview_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTreeview.Popup
        Me.tvwSecurite.ContextMenu.MenuItems.Clear()

        If Me.mclsProfil.IsEditing Then
            Me.mnuTreeview.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuTVSelect, Me.mnuTVUnselect})
        End If
    End Sub
Thanks again.


>Eric,
>
>You shouldn't need to do anything special other than test for the "Edit" mode in the oContextMenu_Popup eventhandler (and then don't create the popup menu items). Are you doing this and it's not working?
>
>~~Bonnie
>
>
>>Hi Bonnie
>>
>>Thanks for your answer. It is almost working all correctly now!
>>
>>The only thing is that I don't want the context menu to be displayed if it is not in "Edit" mode. In my previous, I had a check on that.
>>I have replaced my complete MouseUp event handler with the setting of the treeview's ContextMenu property and with this event to be sure that the correct node is selected:
>>
>>
Private Sub tvwSecurite_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwSecurite.MouseDown
>>    If e.Button = MouseButtons.Right Then
>>        tvwSecurite.SelectedNode = tvwSecurite.GetNodeAt(e.X, e.Y)
>>    End If
>>End Sub
>>
>>
>>
>>Where can I cancel the click now if the user is not in edit mode?
>>
>>
>>
>>>Eric,
>>>
>>>You don't really have to mess with clicks and other mouse events to display and capture ContextMenus. I think that is what's causing you problems (although I haven't used a TreeView with checkboxes in it, but I doubt that this is the problem). I'm sorry to have to post this in C# rather than VB, but I tried to use the C#-to-VB converters and they didn't particularly like the way C# does delegates and event handlers. I hope you can figure it out:
>>>
>>>
>>>// 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
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform