C# WinForms highlight treenode when treeview doesnt have focus

Istrebitel picture Istrebitel · Apr 5, 2012 · Viewed 29.3k times · Source

Gretings.

I'm making an interface to edit scenarios for a game. Basically it consists of events, which have nested conditions and actions. So, i planned using two treeviews - one for selecting the event, and other for selecting the condition/action inside the event to edit.

Now, you see, if i select an event (in left treeview) and then try to select something in the right treeview, the left treeview will stop showing the blue selection rectangle. This is obviously bad because now the user doesnt know which event is he editing!

The only way i found to retain some sort of information about what is the current selection is by using SelectedImageIndex, but thats only one little image that will be different.

Is there any other way to highlight the treenode while there is no focus on the treeview? I know i can just Graphics.DrawRectangle or something, but i heard that drawing should be done in Paint event and treeview has no paint event... So i guess if i draw it on the event of losing focus, and then drag the form out of the screen or something, it will be "erased"?

Anyway, please tell me if you got an idea (other than using a separate icon for selected and not selected treenode)

Thanks!

Answer

aKzenT picture aKzenT · Apr 5, 2012

What you are looking for is the HideSelection property on the TreeView.

From MSDN:

Gets or sets a value indicating whether the selected tree node remains highlighted even when the tree view has lost the focus.

Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.hideselection.aspx

Code:

TreeView.HideSelection = false;