Limiting node zone selection

5 posts, 1 answers
  1. Nathalie
    Nathalie avatar
    4 posts
    Member since:
    Nov 2012

    Posted 15 Nov 2012 Link to this post

    Hi,


    Is there a way to restrict a RadTreeNode selection zone? In other words, I want to select/highlight a node only when the mouse is over the tree node name, and then not being able to select/highlight a node when the mouse is located in a blank space.


    Regards.
  2. Anton
    Admin
    Anton avatar
    167 posts

    Posted 20 Nov 2012 Link to this post

    Hi Nathalie,

    Thank you for writing.

    You will achieve this behavior if you set FullRowSelect property to "false". For example:
    this.radTreeView1.FullRowSelect = false;

    I hope this information helps. If you have further questions, feel free to write back

    All the best,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Nathalie
    Nathalie avatar
    4 posts
    Member since:
    Nov 2012

    Posted 20 Nov 2012 Link to this post

    Thanks for your answer.

    My problem is I do not have the root node showing up in the tree view and I would like to get a general contextual menu while right-clicking in a blank space (and then no node is selected in the tree view).

    When I right-click on a node name, I want its own contextual menu showing up (and not the general contextual menu).

    Nathalie.
  5. Answer
    Anton
    Admin
    Anton avatar
    167 posts

    Posted 23 Nov 2012 Link to this post

    Hello Nathalie,

    Thank you for writing back.

    Here is how you can achieve the desired behavior:

    1.Create your custom TreeViewControl that inherits RadTreeView.
    public class MyCustomTreeViewControl : RadTreeView
    {
        public MyCustomTreeViewControl()
        {
            this.ThemeClassName = typeof(RadTreeView).FullName;
        }
        protected override RadTreeViewElement CreateTreeViewElement()
        {
            return new MyCustomTreeViewElement();
        }
    }

    2.Create a TreeViewElement that inherits RadTreeViewElement and override the ProcessContextMenu method with your custom logic. For example:
    public class MyCustomTreeViewElement : RadTreeViewElement
    {
        protected override bool ProcessContextMenu(Point location)
        {
            RadContextMenu defaultMenu = new RadContextMenu();
            defaultMenu.Items.Add(new RadMenuItem("Item1"));
            defaultMenu.Items.Add(new RadMenuItem("Item2"));
     
            RadTreeNode node = this.GetNodeAt(location);
            TreeNodeElement element = this.GetNodeElementAt(location.X, location.Y);
     
            if (node == null)
            {
                defaultMenu.Show(this.ElementTree.Control, location);
            }
            else
            {
                if (element.ContentElement.ControlBoundingRectangle.Contains(location))
                {
                    node.ContextMenu.Show(this.ElementTree.Control, location);
                }
                else
                {
                    defaultMenu.Show(this.ElementTree.Control, location);
                }
            }
     
            return true;
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadTreeViewElement);
            }
        }
    }

    3. Use the MyCustomTreeViewControl to replace RadTreeView:
    private void Form1_Load(object sender, EventArgs e)
    {
        RadTreeNode Node1 = new RadTreeNode("Node1");
        RadTreeNode Node2 = new RadTreeNode("Node2");
        RadTreeNode Node3 = new RadTreeNode("Node3");
        RadTreeNode Node4 = new RadTreeNode("Node4");
     
        this.myTreeView.FullRowSelect = false;
        this.myTreeView.Nodes.Add(Node1);
        this.myTreeView.Nodes.Add(Node2);
        Node1.Nodes.Add(Node3);
        Node2.Nodes.Add(Node4);
     
        RadContextMenu nodeMenu = new RadContextMenu();
        nodeMenu.Items.Add(new RadMenuItem("NodeMenuItem1"));
        nodeMenu.Items.Add(new RadMenuItem("NodeMenuItem2"));
     
        Node1.ContextMenu = nodeMenu;
        Node2.ContextMenu = nodeMenu;     
    }
    Attached is a sample project that comprises the code above. I hope this helps.

    Let me know if this solution is not suitable for you.

    Should you have any other questions, I will be glad to assist you.

    All the best,
    Anton
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  6. Nathalie
    Nathalie avatar
    4 posts
    Member since:
    Nov 2012

    Posted 23 Nov 2012 Link to this post

    Thank you very much. It works well!

    Regards,
    Nathalie.
Back to Top
UI for WinForms is Visual Studio 2017 Ready