Change back color of previously selected node

8 posts, 0 answers
  1. Sunil
    Sunil avatar
    11 posts
    Member since:
    May 2009

    Posted 14 Sep 2009 Link to this post

    Hi,
    I have added a RadTreeView Control in run time. I am changing the backcolor of the selected node usnig 

    treeView_Selected event. But when I select another node , the colour of previously selected node also remains the same (I want to change the back colour of this previously selected node). 
    Does any one have any idea, how to achieve it? 


     

  2. Robert
    Robert avatar
    82 posts
    Member since:
    Jun 2009

    Posted 14 Sep 2009 Link to this post

    Hello Sunil,

    You should be able to change the color of the previous selected node by using the Selecting event. This event is called before the currently selected node is changed.

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
            } 
     
            private void radTreeView1_Selected(object sender, EventArgs e) 
            { 
                radTreeView1.SelectedNode.BackColor = Color.Red; 
            } 
     
            private void radTreeView1_Selecting(object sender, RadTreeViewCancelEventArgs e) 
            { 
                if (radTreeView1.SelectedNode != null
                    radTreeView1.SelectedNode.BackColor = Color.Blue; 
            } 
        } 

    I hope this helps.

    - Robert
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sunil
    Sunil avatar
    11 posts
    Member since:
    May 2009

    Posted 15 Sep 2009 Link to this post

    Hi Robert,

    Thanks for your reply.

    But this solution is not working for me.
    I am getting radTreeView1.SelectedNode as null in radTreeView1_Selecting event. Another thing is that i am creating radTreeView control in run time, so I don't have access of the instance of radTreeView control in radTreeView1_Selecting event. So Iam using e.Node.TreeView.SelectedNode in the radTreeView1_Selecting  event, which i think is same as radTreeView1.SelectedNode.

     

  5. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 15 Sep 2009 Link to this post

    Hi Sunil,

    Thank you for writing. Just keep a reference to the previously selected node and change its back color as needed. Write again if you need further assistance.

    All the best,
    Victor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Sunil
    Sunil avatar
    11 posts
    Member since:
    May 2009

    Posted 15 Sep 2009 Link to this post

    Hi,
    Could you please provide some code sample regardning how to keep the refrence of selected node? Please keep in mind that treeview is created in run time not in design time.
    Thanks in advance....
  7. Robert
    Robert avatar
    82 posts
    Member since:
    Jun 2009

    Posted 15 Sep 2009 Link to this post

    Hi Sunil,

    I am unsure of why the way you are accessing the TreeView isn't working.. However, you can actually access the RadTreeView in the selecting event by casting the sender parameter as a RadTreeView. Here is an example of creating a RadTreeView control at runtime with support for the Selected and Selecting events.

        public partial class Form1 : Form 
        { 
            public Form1() 
            { 
                InitializeComponent(); 
     
                RadTreeView myTreeView = new RadTreeView(); 
                myTreeView.Nodes.Add(new RadTreeNode("Hello")); 
                myTreeView.Nodes.Add(new RadTreeNode("Goodbye")); 
                myTreeView.Selected += new EventHandler(myTreeView_Selected); 
                myTreeView.Selecting += new RadTreeView.RadTreeViewCancelEventHandler(myTreeView_Selecting); 
     
                this.Controls.Add(myTreeView); 
            } 
     
            void myTreeView_Selected(object sender, EventArgs e) 
            { 
                RadTreeNode node = sender as RadTreeNode; 
                node.BackColor = Color.Red; 
            } 
     
            void myTreeView_Selecting(object sender, RadTreeViewCancelEventArgs e) 
            { 
                RadTreeView treeView = sender as RadTreeView; 
                if (treeView.SelectedNode != null
                    treeView.SelectedNode.BackColor = Color.Blue; 
            } 
        } 

    I hope this helps.

    - Robert
  8. Lorena
    Lorena avatar
    1 posts
    Member since:
    Sep 2016

    Posted 14 Sep in reply to Robert Link to this post

    Hi!, I have 205 Version and not existe the event you post, ¿Do you know how to change backcolor selection in this version?
  9. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 16 Sep Link to this post

    Hi Lorena,

    Thank you for writing.

    I am not exactly sure of the version you are using. You can try handling the SelectedNodeChanging  and SelectedNodeChanged events and keep track of track of the selected nodes. At the same time you would be able to change the required back color: 
    private void radTreeView1_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
    {
        first = second;
        second = e.Node;
    }
     
    RadTreeNode first;
    RadTreeNode second;
    private void radTreeView1_SelectedNodeChanging(object sender, RadTreeViewCancelEventArgs e)
    {
        if (second != null)
        {
            second.BackColor = Color.Red;     
        }
     
        if (first != null)
        {
            first.BackColor = Color.Empty;     
        }
    }

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready