drag and drop from listview to ArcMap TOC

Discussion created by cpicciri on Nov 8, 2012
Hi everyone,

We have a form with a listview control that lists over 1,300 available layers. The form has filters and keyword searching capability to narrow down the results. Once the user finds what the want to load, they select one or more layers and click an OK button. The code behind the the OK button loads all selected layers into ArcMap. This all works great but now my boss wants to fancy it up and have the ability to select one or more layers and drag them into ArcMap like you can do from Windows Explorer for a layer file. I've seen a few similar posts in the old forums but never saw a solution. 

Searching the web I found one promising example at [HTML]http://kiwigis.blogspot.com/2009/05/drag-and-drop-in-arcgis-desktop.html[/HTML]

In their example, they open a GxDialog to allow the user to select one or more layers. The selected layers are added to a listview control on a form and then the user can drag/drop the layer from the form into ArcMap.

I cannot use their approach because that would require getting a reference to each layer in my listview and doing that for 1,300+ layers would surely take a LONG time to complete.

What I am doing is when the user selects a layer from the listview, the listview_click event gets a reference to the selected layer and returns an ILayer. Here begins my problem. In order to follow their code as much as possible, I need to convert the ILayer reference to an  IGxObject. I've spent two days now trying to do that but have not been able to.

Below is the pertinent code from the example.

private void Button_Click(object sender, EventArgs e) 
 // Create GxObjectFilter for GxDialog
 IGxObjectFilter gxObjectFilter = new GxFilterGeoDatasets();

 // Create GxDialog
 IGxDialog gxDialog = new GxDialogClass();
 gxDialog.AllowMultiSelect = true;
 gxDialog.ButtonCaption = "Select";
 gxDialog.ObjectFilter = gxObjectFilter;
 gxDialog.RememberLocation = true;
 gxDialog.Title = "Select one or more feature classes";

 // Declare Enumerator to hold selected objects
 IEnumGxObject enumGxObject = null;

 // Open Dialog
 if (!gxDialog.DoModalOpen(this.Handle.ToInt32(), out enumGxObject)) { return; }
 if (enumGxObject == null) { return; }

 // Get Selected Object (if any)
 IGxObject gxObject = enumGxObject.Next();
 while (gxObject != null) 
  // Get Named Object
  IGxDataset gxDataset = gxObject as IGxDataset;
  if (gxDataset == null) { continue; }
  IDatasetName datasetName = gxDataset.DatasetName;

  // Add to list
  ListViewGroup g = null;
  foreach(ListViewGroup group in this.listViewNames.Groups)
   if (group.Header == Form1.GetDescription(datasetName.Type))
    g = group;
  if (g == null) 
   g = new ListViewGroup(Form1.GetDescription(datasetName.Type), HorizontalAlignment.Left);
  DatasetNameItem d = new DatasetNameItem(datasetName);
  d.Group = g;

  // Get Next Item
  gxObject = enumGxObject.Next();