This topic provides you with a detailed step-by-step guide on how to implement drag and drop functionality in your map.
1. Create a map. For detailed instructions how to do it, please read the "Getting Started" topic.
2. Prepare the list with the images to drag and drop.
2.1 Click on the ListView control in the Toobox panel in Visual Studio while in Design view. Place the control at the desired location in your form and size it according to your needs.
2.2. Create a folder named "Images" in your project. Add the images for the drag and drop there with the "Add existing item" command.
2.3 Initialize a strongly typed List<image> that will hold all the images. Write the following line in the code-behind class of the form that holds the MapView control and the ListView with the images:
C#
![]() |
---|
List<Image> images = new List<Image>(); |
VB.NET
![]() |
---|
Dim images As New List(Of Image)() |
C#
![]() |
---|
// load images //create a list that will hold the images //loop through all loaded image files //create a ListViewItem from each image //add the Image to the image list //set a proper view for the ListView control //create a DecorationLayer over the MapView control |
VB.NET
![]() |
---|
' load images 'create a list that will hold the images 'set the image size 'loop through all loaded image files 'create a ListViewItem from each image 'add the Image to the image list 'add the ListBoxItem to the ListBox 'set a proper view for the ListView control 'create a DecorationLayer over the MapView control |
3. Add event handler for the ItemDrag event of the ListView control.
C#
![]() |
---|
this.lvImages.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.lvImages_ItemDrag); |
VB.NET
![]() |
---|
Me.lvImages.ItemDrag += New System.Windows.Forms.ItemDragEventHandler(Me.lvImages_ItemDrag) |
3.1 Provide the event with data about the index of the image being dragged.
C#
![]() |
---|
private void lvImages_ItemDrag(object sender, ItemDragEventArgs e) |
VB.NET
![]() |
---|
Private Sub lvImages_ItemDrag(sender As Object, e As ItemDragEventArgs) |
4. Handle the DragOver event of the MapView.
C#
![]() |
---|
this.mapView.DragOver += new System.Windows.Forms.DragEventHandler(this.mapView_DragOver); |
VB.NET
![]() |
---|
Me.mapView.DragOver += New System.Windows.Forms.DragEventHandler(Me.mapView_DragOver) |
4.1 Get the location where the image is about to be dropped and if that's a valid location - copy there the file:
C#
![]() |
---|
private void mapView_DragOver(object sender, DragEventArgs e) //get the map coordinates of the specified client point //get the region where the item will be dropped //copy the dragged image into the specified province |
VB.NET
![]() |
---|
Private Sub mapView_DragOver(sender As Object, e As DragEventArgs) 'get the map coordinates of the specified client point 'get the region where the item will be dropped 'copy the dragged image into the specified province |
5. Handle the DragDrop event of the MapView.
C#
![]() |
---|
this.mapView.DragDrop += new System.Windows.Forms.DragEventHandler(this.mapView_DragDrop); |
VB.NET
![]() |
---|
Me.mapView.DragDrop += New System.Windows.Forms.DragEventHandler(Me.mapView_DragDrop) |
5.1 Create a DecorationImage together with a label and place it at the location where it has been dropped.
C#
![]() |
---|
private void mapView_DragDrop(object sender, DragEventArgs e) //get the map coordinate of the specified client coordinate //check if the drop location is a valid region //if this is valid region the image must be copied if (region != null) //create a DecorationImage to add to the DecorationLayer //set the label //add the image //repaint the map |
VB.NET
![]() |
---|
Private Sub mapView_DragDrop(sender As Object, e As DragEventArgs) 'get the map coordinate of the specified client coordinate 'check if the drop location is a valid region 'if this is valid region the image must be copied If region IsNot Nothing Then 'create a DecorationImage to add to the DecorationLayer 'set the label 'add the image 'repaint the map |
6. By now you must have working drag and drop functionality in your map from the kind shown below: