As you probably already noticed you can drag drop items within ArcGIS. For example you can drag a feature class from ArcCatalog to ArcMap or from ArcMap or ArcCatalog to a geoprocessing form. In this post I'm going to show you what has to be done to enable drag drop behavior from ArcCatalog and Windows Explorer to a textbox.
To enable drag drop on a textbox I added event handlers for DragEnter, DragOver and DragDrop for my textbox called textBoxPath. In the DragEnter and DragOver handlers I check whether the dragged object is valid. If this is the case the drag drop effect is set to All. This is a combination of the Copy, Move, and Scroll effect. But its in the DragDrop event handler and more precisely in the helper function GetPaths that the most import stuff happens. As you can see the Data property of the DragEventArgs is processed by the GetPaths method and if any paths to feature classes or tables are found the first path is shown. After that I added a small hack to put the cursor at the end of the text in the textbox.
private void TextBoxPath_DragEnter(object sender, DragEventArgs e)
{
e.Effect = EsriDragDrop.IsValid(e.Data) ? DragDropEffects.All : DragDropEffects.None;
}
private void TextBoxPath_DragOver(object sender, DragEventArgs e)
{
e.Effect = EsriDragDrop.IsValid(e.Data) ? DragDropEffects.All : DragDropEffects.None;
}
private void TextBoxPath_DragDrop(object sender, DragEventArgs e)
{
List<string> paths = EsriDragDrop.GetPaths(e.Data);
TextBox txtBoxPath = (TextBox)sender;
if (paths.Count > 0)
{
// set value of textbox to the first found path
txtBoxPath.Text = esriDragDrop.Paths[0];
// place cursor at the end of the textbox
txtBoxPath.SelectionStart = txtBoxPath.TextLength;
}
}
In the below EsriDragDrop class I placed the IsValid and GetPaths methods. The IsValid method checks whether the IDataObject coming from the drag event contains any valid objects. The GetPaths method retrieves those valid objects and returns the paths to the found feature classes and tables. It uses the IDataObjectHelper interface and its GetNames and GetFiles methods to access the objects in the IDataObject. Note that only feature classes and tables will be returned by my code but this constraint can easily be removed by not checking the Type of the datasetName. I didn't add any functionality to check whether the file path dragged from a Windows Explorer to the textbox was valid but you can implement this by using the Geoprocessor object and its Exists and Describe methods or by trying to open the table or feature class.
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
namespace GisSolved.DragDrop
{
public class EsriDragDrop
{
const string DATAOBJECT_ESRINAMES = "ESRI Names";
public static bool IsValid(IDataObject dataObject)
{
return dataObject.GetDataPresent(DATAOBJECT_ESRINAMES) ||
dataObject.GetDataPresent(System.Windows.Forms.DataFormats.FileDrop);
}
public static List<string> GetPaths(IDataObject dataObject)
{
List<string> foundPaths = new List<string>();
IDataObjectHelper dataObjectHelper = new DataObjectHelperClass();
dataObjectHelper.InternalObject = (object)dataObject;
if (dataObjectHelper.CanGetNames())
{
IEnumName enumNames = dataObjectHelper.GetNames();
IName name;
while ((name = enumNames.Next()) != null)
{
if (name is IDatasetName)
{
IDatasetName datasetName = (IDatasetName)name;
// only accept feature classes and tables
if (datasetName.Type == esriDatasetType.esriDTFeatureClass ||
datasetName.Type == esriDatasetType.esriDTTable)
{
string path = System.IO.Path.Combine(datasetName.WorkspaceName.PathName, datasetName.Name);
foundPaths.Add(path);
}
}
}
}
else if (dataObjectHelper.CanGetFiles())
{
string[] paths = (string[])dataObjectHelper.GetFiles();
foreach (string path in paths)
{
// TODO : Add code here to check if the file path is a valid path
foundPaths.Add(path);
}
}
return foundPaths;
}
}
}
This is all you need to implement drag drop behavior from ArcCatalog or Windows Explorer to your textbox. If you want to implement drag drop from ArcMap to your form I suggest you to read this and this. Any comments or suggestions ? Let me know.
Related posts
DatagridView Tricks
Calling .NET from Python to execute spatial queries
Projecting coordinates with Python and the ArcGIS Projection Engine