I have a TreeView control showing multiple TreeNodes in an organised heirarchy. I want to stop the user selecting the highest level Nodes (this was achieved by using the BeforeSelect Event). I also want to stop the TreeView from highlighting the top level nodes if the user selects them i.e. stop the TreeView from changing the background color of the node and ‘selecting’ it.
The TreeView that I am using is the WinForms version of the control.
Below is the source code I am currently attempting to use:
private void tree_BeforeSelect ( object sender, TreeViewCancelEventArgs e )
{
if ( e.Node.Level == 0 )
{
e.Cancel = true;
}
}
This does de-select the Node but only after a noticible flash (~200ms) which is undesirable.
In addition to your existing code if you add a handler to the MouseDown event on the TreeView with the code and select the node out using it’s location, you can then set the nodes colours.
There is still a slight problem in that the select outline still shows on MouseDown but it atleast stops the blue background and gets you a little further.
HTH
OneSHOT