none
Change node backcolor in treeview

    Question

  • I have created a treeview

    whene i click on any node in treeview it shows the corresponding notes in the texbox next to it

    I want to change the back color of the node clicked

    I able able to do till this

     

    however I am not able to change the backcolor back to default

    I am not able to cycle through the nodes in the treeview

    I would be glad if some one could share tht code or tell me how to cycle through the tree nodes.

    Thursday, July 12, 2007 12:05 AM

Answers

  • There is no need to cycle through the nodes to find the currently selected node.  You can assign the node to a class variable whenever it is selected then reset its backcolor when the next node is selected.

     

    'This code assumes you have a form names Form1 with a treeview control named TreeView1.

    Private DefBackColor As Color

    Private SelectedColor As Color

    Private SelectedNode As TreeNode

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    DefBackColor = TreeView1.BackColor

    SelectedColor = Color.Red

    End Sub

     

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) _

    Handles TreeView1.AfterSelect

    'Set the selected backcolor to the new node when it is selected.

    SelectedNode = e.Node

    SelectedNode.BackColor = Color.Red

    End Sub

     

    Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _

    Handles TreeView1.BeforeSelect

    'Before the new node is selected reset the currently selected one's color back to default.

    If SelectedNode IsNot Nothing Then

    SelectedNode.BackColor = DefBackColor

    End If

    End Sub

    Thursday, July 12, 2007 12:22 PM

All replies

  • There is no need to cycle through the nodes to find the currently selected node.  You can assign the node to a class variable whenever it is selected then reset its backcolor when the next node is selected.

     

    'This code assumes you have a form names Form1 with a treeview control named TreeView1.

    Private DefBackColor As Color

    Private SelectedColor As Color

    Private SelectedNode As TreeNode

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    DefBackColor = TreeView1.BackColor

    SelectedColor = Color.Red

    End Sub

     

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) _

    Handles TreeView1.AfterSelect

    'Set the selected backcolor to the new node when it is selected.

    SelectedNode = e.Node

    SelectedNode.BackColor = Color.Red

    End Sub

     

    Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _

    Handles TreeView1.BeforeSelect

    'Before the new node is selected reset the currently selected one's color back to default.

    If SelectedNode IsNot Nothing Then

    SelectedNode.BackColor = DefBackColor

    End If

    End Sub

    Thursday, July 12, 2007 12:22 PM
  • Thanks a lot

     It worked Smile

    Thursday, July 12, 2007 11:36 PM