Change selected node color and expand it

R

RP

I have a TreeView control. When a user clicks on a node, it must
change color.

I did:

TreeView1.SelectedNode.ForeColor = colors.Blue;

However, it must change the other nodes color to black, if they are in
blue. How to do this?

Secondly, when a user creates a new item for the TreeView, and saves
in the table, I want that the parent node for this newly created node
must be in an Expanded State when the tree is refreshed.
For this I have a GenerateTree() function which reads the values from
table and creates a tree.
 
N

Nicholas Paldino [.NET/C# MVP]

RP,

When you change the color of the current node, you should set a field to
the newly selected node.

Then, you can change the color of the node on the previously stored node
in the field when the new node is selected (just make sure to change the
node that the field points to each time).

As for expanding the parent of the newly added child, you just have to
call the Expand method on the parent node that you added the child to.
 
R

RP

I tried the Expand but it is not working. This way:

TreeNode[] tn = TreeView1.Nodes.Find(strParent,true);
tn[0].Nodes.Expand;
 
N

Nicholas Paldino [.NET/C# MVP]

Why are you doing this? Why not just use the Parent property on the
TreeNode you add to the tree?
 
R

RP

For changing the color to black of the previously selected node, I'm
not getting how to do this. When the user selects a node, I'm able to
use:

TreeView1.SelectedNode.ForeColor = Color.Blue;

But how to change the previous node color to black. I tried to store
this selected node in a temp variable and used:

TreeView1.Nodes[OldNode].ForeColor = Color.Black;

But, it is returning "NullReferenceException".
 
P

Peter Duniho

For changing the color to black of the previously selected node, I'm
not getting how to do this. When the user selects a node, I'm able to
use:

TreeView1.SelectedNode.ForeColor = Color.Blue;

But how to change the previous node color to black. I tried to store
this selected node in a temp variable and used:

TreeView1.Nodes[OldNode].ForeColor = Color.Black;

But, it is returning "NullReferenceException".

Just save the node itself. Variable type TreeViewNode, assign it to
SelectedNode after you've set the color, and then just before you set the
color the next time, use that variable's ForeColor property to set the
color back to Black.

TreeView1.Nodes[OldNode] is going to require that you save a node index
rather than a reference, and that the node be a root node. If these
conditions hold, then I'd think the code you posted should work, assuming
you wrote the rest of the code correctly (you may not have, but you didn't
post the surrounding code, so we have no way to know). So if the code
doesn't work, either those conditions don't hold or you messed something
else up.

There's not enough information in your post to determine the exact nature
of the problem though.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top