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

Change back color of previously selected node

7 Answers 1024 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Sunil
Top achievements
Rank 1
Sunil asked on 14 Sep 2009, 01:05 PM
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? 


 

7 Answers, 1 is accepted

Sort by
0
Robert
Top achievements
Rank 1
answered on 14 Sep 2009, 04:38 PM
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
0
Sunil
Top achievements
Rank 1
answered on 15 Sep 2009, 05:05 AM

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.

 

0
Victor
Telerik team
answered on 15 Sep 2009, 07:18 AM
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.
0
Sunil
Top achievements
Rank 1
answered on 15 Sep 2009, 07:58 AM
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....
0
Robert
Top achievements
Rank 1
answered on 15 Sep 2009, 10:37 PM
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
0
Lorena
Top achievements
Rank 1
answered on 14 Sep 2016, 10:56 PM
Hi!, I have 205 Version and not existe the event you post, ¿Do you know how to change backcolor selection in this version?
0
Hristo
Telerik team
answered on 16 Sep 2016, 02:08 PM
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.
Tags
Treeview
Asked by
Sunil
Top achievements
Rank 1
Answers by
Robert
Top achievements
Rank 1
Sunil
Top achievements
Rank 1
Victor
Telerik team
Lorena
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or