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

Disable Right Click Selection Change

3 Answers 287 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 05 Oct 2011, 05:44 PM
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

3 Answers, 1 is accepted

Sort by
0
Ivan Petrov
Telerik team
answered on 11 Oct 2011, 07:52 AM
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.

0
Marcus
Top achievements
Rank 1
answered on 03 Nov 2011, 12:01 PM
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

0
Ivan Petrov
Telerik team
answered on 07 Nov 2011, 11:43 AM
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.

Tags
Treeview
Asked by
John
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Marcus
Top achievements
Rank 1
Share this question
or