This is a migrated thread and some comments may be shown as answers.

Treeview DefaultContextMenu event

4 Answers 141 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Kurt Bhagat
Top achievements
Rank 1
Kurt Bhagat asked on 19 Sep 2010, 11:00 PM
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.

4 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 23 Sep 2010, 03:34 PM
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


0
Nikolay
Telerik team
answered on 24 Sep 2010, 09:15 AM
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
0
Kurt Bhagat
Top achievements
Rank 1
answered on 29 Sep 2010, 03:23 PM
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 )
0
Nikolay
Telerik team
answered on 04 Oct 2010, 09:32 AM
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
Tags
Treeview
Asked by
Kurt Bhagat
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Nikolay
Telerik team
Kurt Bhagat
Top achievements
Rank 1
Share this question
or