Search code examples
c#winformstreeviewtreenode

C# WinForms highlight treenode when treeview doesn't have focus


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 on 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 doesn't 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 that's 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 use 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).


Solution

  • 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;