Explorer like selection


What would be the easiest way to make a selection like the one in Windows Explorer for iconic nodes (image + text)?
Basicaly the selection in explorer is highlighting the image and the text with some kind of dark blue.


You’ll want to override the SelectionObject property to just return the whole GoIconicNode, and to override AddSelectionHandles and RemoveSelectionHandles to make the appearance changes you want. The reason for overriding the SelectionObject property is that the standard behavior has it returning just the Icon, whereas you want to highlight both the Icon and the Label, so it’s best to just return the node and change the behavior of the two Add/RemoveSelectionHandles methods.
For the GoText label, you just need to have set the BackgroundColor and then you can toggle the TransparentBackground property. It’s possible that you’ll also need to change the TextColor, if keeping the original TextColor would make it hard to read against the BackgroundColor.
For the GoImage icon, I’m not sure what Windows does for Explorer icons. I believe the right thing to do is to replace the GoImage that a standard GoIconicNode has with your own subclass of GoImage that overrides Paint in order to draw the Image with the right kind of color and transparency. For more details, look at the topic:
To make use of your new GoImage subclass, you’ll want to override GoIconicNode.CreateIcon.


Thank for your tips I will try this


The implementation of the explorer like selection worked very well as sugested. Thank you again for your help.

Just one problem:

AddSelectionHandles and RemoveSelectionHandles seems to be called many
times for a GoNode. So the only solution to know if the object is
selected was to use selection.Contains(this) - slow when the selection
is big - in the method implementation.

I was expecting that AddSelectionHandles to be called once when the
object is selected and then RemoveSelectionHandles called when the
object is deselected.

The documentation does not provide any explanation for this. This is the correct approach?


Yes, it may be called at various times, but at any one gesture I wouldn’t expect it to be called many times for each selected node.
Perhaps you are surprised that it is called when the view loses focus. This is needed to allow the selection to change appearance based on focus. See the documentation for GoSelection.OnGotFocus and OnLostFocus. If you would like to avoid that, set GoView.NoFocusSelectionColor to be the same as GoView.PrimarySelectionColor.
GoSelection.Contains is actually very fast–it’s a hash table lookup. GoCollection.Contains is indeed a linear search, but GoSelection duplicates the information in a hash table for speed where it can be used in that manner.