English   Russian  



Subscribe



Partners

Home :: Blog :: Drag&Drop in Heapar .NET FileListView

Drag&Drop in Heapar .NET FileListView


Today I'm going to show you how to add Drag&Drop feature to your application, that uses Heapar .NET FileListView Control. Create a new application and add FileListView control on the form. For this example we need any other control we will use to drag files on. I decided to use Panel control. Change the AllowDrop property in the Panel control to true. This control will be docked at the botom of the form.

Window Form for Example

Create ItemDrag event hendler for the FileListView control. Let's write the next code in this event handler:

 private void fileListView1_ItemDrag(object sender, ItemDragEventArgs e)
{
  try
  {
    if (e.Button == MouseButtons.Right)
      return;
    ListViewItem item = (ListViewItem)e.Item;
    DoDragDrop(fileListView1.FileSystemItems[item.Index], DragDropEffects.Copy);
  }
  catch (Exception ex)
  {
    MessageBox.Show(this, ex.Message);
  }	
}

The main purpose of the code is to get the Item user want to drag and start dragging the item with DoDragDrop function. Now create DragOver and DragEnter event hendlers for the Panel control. Let's write the next code in this event handler:

 private void panel1_DragOver(object sender, DragEventArgs e)
{
   if (e.Data == null)
  {
    e.Effect = DragDropEffects.None;
    return;
  }
  else if (e.Data.GetDataPresent("heaparessential.HeaparFileSystem.ShellItem"))
    e.Effect = DragDropEffects.Copy;
  else
    e.Effect = DragDropEffects.None;
}

 private void panel1_DragEnter(object sender, DragEventArgs e)
{
   e.Effect = e.AllowedEffect;
}

In this event handler we have to fill e.Effect property with correct value. If user drag over Panel the item with correct data type, we put to e.Effect property DragDropEffects.Copy value.

The last one event handler we need is DragDrop for Panel control:

private void panel1_DragDrop(object sender, DragEventArgs e)
{
  ShellItem shellitem = 
    (ShellItem)e.Data.GetData("heaparessential.HeaparFileSystem.ShellItem");
  if (shellitem == null)
    return;
  else
    label1.Text = shellitem.Path;
}

Next time I'm going to show, how to integrate system Drag&Drop feature to you application. N this case your


Copyright © Heapar Software 2009-2010. All rights reserved
Source code and libraries for .NET software developers
Legal Notices | Privacy Policy