Add Data Using ComboBox

2697
2
Jump to solution
09-25-2019 07:27 AM
EmmanuelJohn
New Contributor II

Hello everyone,

Looking for help with adding data (Feature Class, Layer File and Web Service) using Combo box C# addin module for ArcPro SDK for .Net through Visual Studio. I have a code created, but it does not do what I want. Instead of adding a specific layer upon selection; it adds all the layers listed in the OnSelectionChange part. How do I modify the OnSelection code, so only specific layer that is selected in the combo box as an item would be added to the active map. For this example I'm using web services. Thank you 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace Test
{
    internal class ComboBox1 : ComboBox
    {
        private bool _isInitialized;

        public ComboBox1()
        {
            UpdateCombo();
        }

        private void UpdateCombo()
        {
            // TODO – customize this method to populate the combobox with your desired items  
            if (_isInitialized)
                SelectedItem = ItemCollection.FirstOrDefault(); //set the default item in the comboBox


            if (!_isInitialized)
            {
                Clear();
                {
                    Add(new ComboBoxItem("Item1"));
                    Add(new ComboBoxItem("Item2"));
                }
                _isInitialized = true;
            }
            Enabled = true;
        }
        protected override async void OnSelectionChange(ComboBoxItem item)
        {
            {
                ComboBoxItem Item1 = null;
                if (item == Item1)
                    return;
                string url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/Highlands_NJ/MapServer";

                Uri uri = new Uri(url);
                await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));

            }

            {
                ComboBoxItem Item2 = null;
                if (item == Item2)
                    return;
                string url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/AdminBound_NJ/MapServer";

                Uri uri = new Uri(url);
                await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));
            }
        }
    }
}
1 Solution

Accepted Solutions
JimmyBowden
Occasional Contributor II

In your code you are only exiting (returning) when the selected item is equal to null (which it will never be).

Try this instead: 

protected override async void OnSelectionChange(ComboBoxItem item)
 {
 string url = "";
 if (item.Text.Equals("Item1"))
 {
 url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/Highlands_NJ/MapServer";
 }
 else if (item.Text.Equals("Item2"))
 {
 url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/AdminBound_NJ/MapServer";
 }
 else
 { return; }
 Uri uri = new Uri(url);
 await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));

}

View solution in original post

2 Replies
JimmyBowden
Occasional Contributor II

In your code you are only exiting (returning) when the selected item is equal to null (which it will never be).

Try this instead: 

protected override async void OnSelectionChange(ComboBoxItem item)
 {
 string url = "";
 if (item.Text.Equals("Item1"))
 {
 url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/Highlands_NJ/MapServer";
 }
 else if (item.Text.Equals("Item2"))
 {
 url = @"https://maps.nj.gov/arcgis/rest/services/Basemap/AdminBound_NJ/MapServer";
 }
 else
 { return; }
 Uri uri = new Uri(url);
 await QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(uri, MapView.Active.Map));

}
EmmanuelJohn
New Contributor II

Thank You Jimmy It worked!

0 Kudos