Search code examples
c#winformstreeviewgoogle-earth-plugin

How do I keep the KMLTreeView item 'selected' when the TreeView loses focus?


In Google Earth the background color of a selected item in the KMLTreeView dims when clicking on the Globe. In my C# based application the TreeView Node loses all color so I do not know which item is selected.

Similarly, I would like to have the treeview node highlight when I click on its associated placemark as also happens in GE.

I assume this is the default behavior so I must not be associating the placemarks with the kmltreeview properly. Below is the code I use for creating and adding the node to the globe as well as the kmltreeview control. Is there something I'm doing wrong or not doing to be able to use the default behavior?

Thanks!

dynamic placemark = KmlHelpers.CreatePlacemark(ge1,
                                               Coord,
                                               d.sSerialNumber,
                                               d.sNickname,
                                               "Device Type: " + d.sName + "<p>" +
                                               "IP Address: " + d.sIPAddress + "<p>" +
                                               "ESN: " + d.sSerialNumber + "<p>" +
                                               "<a href=\"http://localhost/index.html#"
                                               + d.sSerialNumber + "\">Details</a>");

var styleMap = ge1.createStyleMap("");

// Create normal style for style map.
var normalStyle = ge1.createStyle("");
var normalIcon = ge1.createIcon("");
normalIcon.setHref("http://maps.google.com/mapfiles/kml/shapes/truck.png");
normalStyle.getIconStyle().setIcon(normalIcon);

// Create highlight style for style map.
var highlightStyle = ge1.createStyle("");
var highlightIcon = ge1.createIcon("");
highlightIcon.setHref("http://maps.google.com/mapfiles/kml/shapes/truck.png");
highlightStyle.getIconStyle().setIcon(highlightIcon);
highlightStyle.getIconStyle().setScale(2.0);
styleMap.setNormalStyle(normalStyle);
styleMap.setHighlightStyle(highlightStyle);

// Apply stylemap to a placemark.
placemark.setStyleSelector(styleMap);

kmlTreeView.ParseKmlObject(placemark);

Solution

  • The KmlTreeView inherits from the standard TreeView control, as such you can use the HideSelection property. By default this is set to True, however...

    When this property is set to false, selected nodes in the TreeView control remain highlighted in a different color than the current selection color when the TreeView control loses focus. You can use this property to keep items that are selected by the user visible when the user clicks a different control on the form or moves to a different window.

    To do this in code, for example:

    kmlTreeView.HideSelection = False;
    

    Also, the property can be set in the visual designer for the control, simply select the KmlTreeView, then view the properties. Finally, double click on HideSelection to set it to False

    UPDATE:

    For the second part of the question, highlighting a node when the coorisponding feature is clicked, you would need to write a custom event handler and then use the KmlTreeView's GetNodeById method. Some thing like the following should work.

    private void GEWebBrowser1OnPluginReady(object sender, GEEventArgs geEventArgs)
    {
        this.geWebBrowser1.AddEventListener(geEventArgs.ApiObject.getGlobe(), EventId.MouseDown);
        this.geWebBrowser1.KmlEvent += geWebBrowser1_KmlEvent;
    }
    
    void geWebBrowser1_KmlEvent(object sender, GEEventArgs e)
    {
        // the feature that the mousedown event fired on
        dynamic feature = e.ApiObject.getTarget();
    
        // If you have other events added you will need some conditional logic here
        // to sort out which event has fired. e.g.
        // if(e.EventId == EventId.MouseDown)
        // if(GEHelpers.IsApiType(feature, ApiType.KmlPlacemark);
        // etc..
    
        string id = feature.getId(); // the features id
        KmlTreeViewNode node = myKmlTreeView.GetNodeById(id);   // find the corresponding node...    
        if(node == null) { return; } // no corresponding node...
    
        // set the selected node to the feature node.
        myKmlTreeView.SelectedNode = node; 
    
        // make sure the node is visible in the treeview
        if (!node.IsVisible)
        {
            node.EnsureVisible();
        }
    }