What to do when TreeViews or ListViews don’t show selection or inactive selection

While working on a community app project (you’ll hear more on this later), I needed help setting the selection (and inactive selection) on a tree view. 

Just a refresher from my first ever blog post,

Explanation of Focus and Selection

Focus: Only one control or UI Element can take focus at a time. If I were to press a key on the keyboard, the keystroke can only be sent to that control with focus.

Selection: Multiple controls can have multiple items selected. List boxes can have as many or as few items selected.

Inactive Selection: When focus moves away from the list box (with selected items) to another control, those list box items are said to have inactive selection:

Active Selection: The selected list box item with focus. Hence, only one item can have Active Selection.

Suppose you have a form that consists of a button and tree view or list view with several nodes. Start with selecting one of the nodes either by clicking to tabbing/arrowing to it. When you tab away, the selection will disappear. The same will happen via code if you try to select a node. The node is actually selected, but there’s no visible indication.

To fix this, there’s a property called HideSelection.  After you drop your list view or tree view onto the form, set HideSelection = False. Now, you’ll see the selection or inactive selection whether you’re selecting via code or tabbing away from the control.

Same goes for Standard Win32 Controls. For example, on a standard Win32 tree view control, you’ll need to set the TVS_SHOWSELALWAYS style bit.

And I thought I had retired this category feed. Old habits die hard. <grins>

Comments (3)

  1. Do you think there’s ever a case where HideSelection should be true?

  2. saraford says:

    I’m sure there’s probably a case or two out there when HideSelection should be true, but i can’t think of any off the top of my head. The only thing coming to mind right now is suppose you need to select some nodes via code, and you don’t want it to flash (first nodes selected, then second, then third, and so forth), so you set HideSelection = true. Kinda a weak example, but best i can come up with right now.

    I would have to ask the people who created the standard Win32 List View and Tree View controls the historical reasons why the default is the way it is, because the .NET ones follow what they do.

  3. Babu says:

    How can the IAccessible can be implemented?

Skip to main content