AnsweredAssumed Answered

TreeView in winform bugs when clicking outside of it

Question asked by anjen96 on Dec 17, 2018

I have a form with a TreeView in which the user can select different maps to be loaded into ArcMap. 

 

The issue is that if clicking outside of the form window, the last selection gets "locked up", and then it keeps selecting that no matter where you click on the screen. 

 

I fixed it by displaying it with frm.showDialog(), but this is not a perfect scenario either. 

 

The options are being generated by a switch like this one: 

 

private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
    ClsMaps wsf2 = (ClsMaps)new ClsMaps();
    switch (e.Node.Name)
    {
        case "Option1":
            tbMessages.Text = "Loading : " + e.Node.Name;
            wsf2.GetMaps(@"Option1", @"Some\Path\To\Option1_geotiff\Somewhere",     @"indesOption1", @"FilePath");
            break;
         case "Option2":
             tbMessages.Text = "Loading : " + e.Node.Name;
             wsf2.GetMaps(@"Option2", @"Some\Path\To\Option2_geotiff\Somewhere", @"indexOption2", @"FilePath");
             break;
         case "Option3":
             tbMessages.Text = "Loading : " + e.Node.Name;
             wsf2.GetMaps(@"Option3", @"Some\Path\To\Option3_geotiff\Somewhere", @"indexOption3", @"FilePath");
             break;
         case "Option4":
             tbMessages.Text = "Loading : " + e.Node.Name;
             wsf2.GetMaps(@"Option4", @"Some\Path\To\Option4_geotiff\Somewhere", @"indexOption4", @"FilePath");
             break;
         default:
             tbMessages.Text = "Loading : " + e.Node.Name;
             LoadLyrFromPath(e.Node.Name);
             break;
     }
     treeView.SelectedNode = null;
}

The switch is a lot bigger than this, but this is the structure, no reason to post the full length of it. 

 

The GetMaps method being called in most of the cases is this: 

public void GetMaps(string strGrpLayer, string strIndexPath, string strIndexName, string strIndexColonName, string strType = "TIF")
{
    try
    {
        // delete grouplayer if it is already there
        IActiveView activeView = ArcMap.Document.ActiveView;
        IEnvelope envelope = activeView.Extent;
        ESRI.ArcGIS.Carto.IMap focusMap1 = ArcMap.Document.FocusMap;
        int layerCount1 = focusMap1.LayerCount;

        for (int i = 0; i < layerCount1; i++)
        {
            ILayer layer = focusMap1.Layer[i];
            string layerName = layer.Name;
            if (layer is IGroupLayer && strGrpLayer.Equals(layerName))
            {
                ArcMap.Document.FocusMap.DeleteLayer(layer);
                break;
            }
        }
        //Open index map
        IWorkspace ws = null;
        IWorkspaceFactory wsf = (IWorkspaceFactory)new ShapefileWorkspaceFactory();

        ws = wsf.OpenFromFile(strIndexPath, 0);
        IFeatureWorkspace featureworkspace = (IFeatureWorkspace)ws;
        IFeatureClass fc = featureworkspace.OpenFeatureClass(strIndexName);

        // Create the spatial filter and set its spatial constraints.
        ISpatialFilter spatialFilter = new SpatialFilterClass();
        spatialFilter.Geometry = envelope;
        spatialFilter.GeometryField = fc.ShapeFieldName;
        spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
        IFeatureCursor featureCursor = fc.Search(spatialFilter, true);
        IFeature Feat1 = featureCursor.NextFeature();

        //Create grouplayer
        IGroupLayer groupLayer1 = new GroupLayer();
        groupLayer1.Name = strGrpLayer;
        groupLayer1.Expanded = false;

        //open a raster dataset from a file system
        Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory");
        IWorkspaceFactory wsFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);

        int intNumFeatures = 0;
        //Add all raster files to grouplayer
        while (Feat1 != null)
        {
            //
            int intColnr = Feat1.Fields.FindField(strIndexColonName);
            string strTest = Feat1.get_Value(intColnr).ToString();
            if (strTest.Length == 0 || strTest.Length == 1)
            { }
            else
            {
                if (strType == "TIF" || strType == "AnotherStringType" || strType == "AThirdStringType")
                {
                    string strPath = "";
                    string strFileName = "";

                    if (strType == "TIF")
                    {
                        strPath = System.IO.Path.GetDirectoryName(strTest);
                        strFileName = System.IO.Path.GetFileName(strTest);
                    }
                    else if (strType == "AnotherStringType")
                    {
                        strPath = @"Some\Path\To\Somewhere";
                        strFileName = strTest;
                    }
                    else if (strType == "AThirdStringType")
                    {
                        strPath = @"Some\Path\To\Somewhere";
                        strFileName = strTest;
                    }

                    if (File.Exists(strPath + "\\" + strFileName))
                    {
                        IRasterWorkspace2 rasterWS = (IRasterWorkspace2)wsFactory.OpenFromFile(strPath, 4);

                        IRasterDataset rds = rasterWS.OpenRasterDataset(strFileName);
                        IRasterLayer rasterLayer = new RasterLayerClass();

                        rasterLayer.CreateFromDataset(rds);

                        if (strGrpLayer == "SomeGroupLayer")
                        ////show sentinel2 correct
                        {
                            RasterRGBRendererClass rasterRGBRenderer1 = new RasterRGBRendererClass();
                            rasterRGBRenderer1.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_NONE;

                            rasterRGBRenderer1.UseGamma = true;

                            rasterRGBRenderer1.RedBandIndex = 3;
                            rasterRGBRenderer1.GreenBandIndex = 0;
                            rasterRGBRenderer1.BlueBandIndex = 3;

                            // laver array af doubles med 3 pladser, og tilføjer 1 til index 0, 2 til index 1 og 1 til index 2
                            double[] gammas = new double[3];
                            gammas[0] = 4;
                            gammas[1] = 3;
                            gammas[2] = 2;
                            rasterRGBRenderer1.GammaValue = gammas;

                            rasterLayer.Renderer = rasterRGBRenderer1;
                        }
                        //add raster to grouplayer
                        groupLayer1.Add(rasterLayer);
                    }
                    else
                    {
                        MessageBox.Show("Denne fil kan ikke findes : " + strPath + "\\" + strFileName + System.Environment.NewLine + "      Kontakt en skriftklog og han kan hjælpe dig. Husk kontanter !");
                    }
                }
                else if (strType == "SHP")
                {
                    //strTest = System.IO.Directory.GetParent(strIndexPath).ToString() + "\\" + strTest;
                    string[] filePaths = System.IO.Directory.GetFiles(strTest, "*.shp");
                    int i2 = filePaths.Count();
                    for (int i = 0; i <= i2 - 1; i++)
                    {
                        string strPath2 = filePaths[i];
                        string strPath = System.IO.Path.GetDirectoryName(strPath2);
                        string strFileName = System.IO.Path.GetFileName(strPath2);

                        IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
                        IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)
                          workspaceFactory.OpenFromFile(strPath, 0);
                        // Create a new FeatureLayer and assign a shapefile to it
                        IFeatureLayer featureLayer = new FeatureLayerClass();
                        featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(strFileName);
                        ILayer layer = (ILayer)featureLayer;
                        layer.Name = featureLayer.FeatureClass.AliasName;

                        groupLayer1.Add(layer);
                    }
                }
            }
            intNumFeatures = intNumFeatures + 1;
            Feat1 = featureCursor.NextFeature();

            if (intNumFeatures == 10)
            {
                var varResultat = MessageBox.Show(@"There is more then 10 images in this request. Do you want to load all images?", "Header", MessageBoxButtons.YesNo);

                // If the no button was pressed ...
                if (varResultat == DialogResult.No)
                {
                    // cancel the closure of the form.
                    break;
                }
            }
        }
        //add grouplayer to arcgis
        ArcMap.Document.FocusMap.AddLayer(groupLayer1);
        ArcMap.Document.FocusMap.MoveLayer(groupLayer1, ArcMap.Document.FocusMap.LayerCount - 1);
    }
    catch (InvalidCastException e)
    {
        MessageBox.Show(e.Message);
    }
}


The method being called in the default is this: 

public void LoadLyrFromPath(string strPath2)
{
    try
    {
        if (!System.IO.File.Exists(strPath2)) return;

        IActiveView activeView = ArcMap.Document.ActiveView;
        IMap map = activeView.FocusMap;
        IGxLayer gxLayerCls = new GxLayer();
        IGxFile gxFile = (IGxFile)gxLayerCls;

        //Set the path for where the layer file is located on disk.
        gxFile.Path = strPath2;
        map.AddLayer(gxLayerCls.Layer);
        map.MoveLayer(gxLayerCls.Layer, map.LayerCount - 1);
    }
    catch (InvalidCastException e)
    {
        tbMessages.Text = e.Message;
    }
}

Outcomes