Disable Right Click Selection Change

4 posts, 0 answers
  1. John
    John avatar
    1 posts
    Member since:
    Nov 2007

    Posted 05 Oct 2011 Link to this post

    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

  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 11 Oct 2011 Link to this post

    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.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Marcus
    Marcus avatar
    17 posts
    Member since:
    Aug 2012

    Posted 03 Nov 2011 Link to this post

    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

  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 07 Nov 2011 Link to this post

    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.

Back to Top