Treeview nodes won't highlight using PopulateOndemand
I have a treev开发者_StackOverflow社区iew and I want to highlight (change the text color) of a node in the treeview once that node has been selected. This isnt working for me for some reason. when I select a node nothing happens, but when I click the plus on the same node I just selected...it highlights...and even then when I click any of the childnodes, nothing happens and the root node stays highlighted always. Can anyone point me in the right direction...I'm using c#.
The following works for me. Please note that I cancel the actual selection though, since otherwise the selection highlight would hide my highlightning. So you might have to keep track of which node is selected manually.
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
Dehighlight(treeView1.Nodes);
e.Node.ForeColor = Color.Red;
e.Cancel = true;
}
private void Dehighlight(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
node.BackColor = Color.White;
node.ForeColor = Color.Black;
Dehighlight(node.Nodes);
}
}
精彩评论