Treeview DefaultContextMenu event

5 posts, 0 answers
  1. Kurt Bhagat
    Kurt Bhagat avatar
    2 posts
    Member since:
    Nov 2009

    Posted 19 Sep 2010 Link to this post

    I have a treeview that is dynamically populated with nodes.   I have enabled the default context menu for the treeview.  When I use the default context menu to delete a node, how do I get a reference to the radtreenode object that is being deleted?   There appear to be no standard event handlers for winforms treeview default context menus.

    Thanks.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Sep 2010 Link to this post

    Hi, 

    I'm surprised there is not a NodeDeleted, NodeDeleting event, but this should work for you. 

    1: define a RadItem to be able to handle events
    Private WithEvents m_DeleteContextMenu As RadItem

    2: Handle the ContextMenuShowing event. You can then set the context menu to be the m_DeleteContextMenu
    Private Sub RadTreeView1_ContextMenuShowing(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.ContextMenuShowingEventArgs) Handles RadTreeView1.ContextMenuShowing
        For Each item As RadItem In e.ContextMenu.Items
            If String.Equals("DELETE", item.Text.ToUpperInvariant()) Then
                m_DeleteContextMenu = item
                Exit For
            End If
        Next
    End Sub

    3: You can then handle events on that m_DeleteContextMenu and capture the mouse down. 
    Private Sub DeleteContextMenu_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs) Handles m_DeleteContextMenu.MouseDown
        ' Example - set a label to be the node text of the deleted node
        Me.RadLabel1.Text = Me.RadTreeView1.SelectedNode.Text
    End Sub

    Hope that helps
    Richard


  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 24 Sep 2010 Link to this post

    Hi guys,

    Richard, thank you for your assistance. I have updated your Telerik points for the comminity efforts.

    Kurt, you can use the solution that Richard has suggested for your scenario.

    As to the RadTreeView events, we do not have a global event for the Delete operation of nodes at all levels. However, you can subscribe to the CollectionChanged/CollectionChanging events of the Nodes collection that each node has for its subnodes:
    public Form1()
    {
        InitializeComponent();
      
        this.radTreeView1.AllowDeleteInContextMenu = true;
        this.radTreeView1.AllowDefaultContextMenu = true;
      
        for (int k = 0; k < this.radTreeView1.Nodes.Count; k++)
        {
            CollectionChangedSubscriber(this.radTreeView1.Nodes[k]);
        }
    }
      
    private void CollectionChangedSubscriber(RadTreeNode node)
    {
        if (node.Nodes.Count > 0)
        {
            node.Nodes.CollectionChanged += new Telerik.WinControls.Data.NotifyCollectionChangedEventHandler(Nodes_CollectionChanged);
            node.Nodes.CollectionChanging += new Telerik.WinControls.Data.NotifyCollectionChangingEventHandler(Nodes_CollectionChanging);
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                CollectionChangedSubscriber(node.Nodes[i]);
            }
        }
    }
      
    void Nodes_CollectionChanging(object sender, Telerik.WinControls.Data.NotifyCollectionChangingEventArgs e)
    {
        // sample: if the node starts with 'Node2', it should not be deleted
        RadTreeNode nodeToBeDeleted = (RadTreeNode)e.OldItems[0];
        if (nodeToBeDeleted.Text.ToUpper().StartsWith("NODE2"))
        {
            e.Cancel = true;
        }
    }
      
    void Nodes_CollectionChanged(object sender, Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e)
    {
        RadTreeNode deletedNode = (RadTreeNode)e.NewItems[0];
    }

    I hope this helps. If you have additional questions, feel free to contact me.

    Greetings,
    Nikolay
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Kurt Bhagat
    Kurt Bhagat avatar
    2 posts
    Member since:
    Nov 2009

    Posted 29 Sep 2010 Link to this post

    Thanks to you both.   I agree with Richard in that I am surprised that the winforms standard context menu does not already have mapped events ( I beleive this is the case in aspx/silverlight )
  6. Nikolay
    Admin
    Nikolay avatar
    1803 posts

    Posted 04 Oct 2010 Link to this post

    Hi Kurt Bhagat,

    Thank you for getting back to me.

    Could you please describe the context menu events that you need to handle and which you think should be exposed? Generally, we have the ContextMenuShowing event which allows you to implement your scenario, just like Richard has demonstrated.

    I proposed an answer where the Nodes.CollectionChanged and Nodes.CollectionChanging events are used in regards to Richard's feedback on NodeDeleted and NodeDeleting events.

    Your feedback will allow us to improve our WinForms suite further.

    Greetings,
    Nikolay
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WinForms is Visual Studio 2017 Ready