Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > Disable Right Click Selection Change

Not answered Disable Right Click Selection Change

Feed from this thread
  • John avatar

    Posted on Oct 5, 2011 (permalink)

    By default the tree view will change selection by clicking on a item with left or right mouse, I need to prevent the selected item from changing when a item is clicked with the right mouse, instead i need to hilight the item and pop a context menu.
    Also where do you change the color of the selected background?
    Thanks

    Reply

  • Ivan Petrov Ivan Petrov admin's avatar

    Posted on Oct 11, 2011 (permalink)

    Hi John,

    Thank you for writing.

    You can cancel the selection with right mouse button in the RadTreeView SelectedNodeChanging event using the following code:

    void radTreeView1_SelectedNodeChanging(object sender, RadTreeViewCancelEventArgs e)
    {
      if (Control.MouseButtons == MouseButtons.Right)
      {
        e.Cancel = true;
      }
    }
    In the MouseDown event you can highlight the node you click and show your context menu. Here is a code snippet on how to do that:
    void radTreeView1_MouseDown(object sender, MouseEventArgs e)
    {
      if (e.Button == MouseButtons.Right)
      {
        RadElement element = this.radTreeView1.ElementTree.GetElementAtPoint(e.Location);
        if (!(element is TreeNodeElement))
        {
          element = element.FindAncestor<TreeNodeElement>();
        }
     
        if (element  as TreeNodeElement != null)
        {
          ((TreeNodeElement)element).DrawFill = true;
          ((TreeNodeElement)element).BackColor = Color.Red;
        }
      }
    }
     
    void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
    {
      e.NodeElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
      e.NodeElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
    }
    You need the code in the ItemFormatting event to remove the highlighting of the item.

    I hope this will be useful for you. If you need further assistance, I would be glad to help. All the best,
    Ivan Petrov
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

    Reply

  • Marcus avatar

    Posted on Nov 3, 2011 (permalink)

    I did solve this in a different way:
    Private m_lstSelectedNodes As New List(Of RadTreeNode)

    Private
    Sub rtvTest_SelectedNodeChanged(sender As Object, e As Telerik.WinControls.UI.RadTreeViewEventArgs) Handles rtvTask.SelectedNodeChanged
     
        If Control.MouseButtons <> Windows.Forms.MouseButtons.Left Then
            For Each rtvnNode As RadTreeNode In m_lstSelectedNodes
                rtvnNode.Selected = True
            Next
     
            m_lstSelectedNodes.Clear()
        End If
     
    End Sub
     
    Private Sub rtvTest_SelectedNodeChanging(sender As Object, e As Telerik.WinControls.UI.RadTreeViewCancelEventArgs) Handles rtvTest.SelectedNodeChanging 
        If Control.MouseButtons = Windows.Forms.MouseButtons.Right Then
            m_lstSelectedNodes.Clear()
            For Each rtvnNode As RadTreeNode In rtvTask.SelectedNodes
                m_lstSelectedNodes.Add(rtvnNode)
            Next
     
            e.Cancel = True
        End If
    End Sub

    Reply

  • Ivan Petrov Ivan Petrov admin's avatar

    Posted on Nov 7, 2011 (permalink)

    Hi Marcus,

    Thank you for sharing your solution.

    The solution you have came up with is perfectly legal with only one minor draw back - the memory you need for the selected nodes collection you have to keep and the triggering of the selection events again. When you set the Selected property of a node, you trigger all the events that follow a selection, involving the events in which you trigger it.

    I hope this will be useful for you. Should you need further assistance, feel free to write back.

    Best wishes,
    Ivan Petrov
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > Disable Right Click Selection Change
Related resources for "Disable Right Click Selection Change"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]