BrainBeast

Hunt down knowledge with our help

working-womanDeveloping desktop WPF or WinForm projects, you can stumble upon an idea of creating drag & drop functionality. Drag-and-Drop effect will simplify file manipulation between browser, computer file system and desktop applications.

To understand the following sample you should already know:

  1. Basic priciples of Object-oriented programming
  2. How to create WPF or WinForm projects

To make it work you should get on with installed Visual Studio.

Drag & Drop in WPF .Net Core/Framework

  1. Create WPF project
  2. Add ListView to the main window
  3. Add Drop and MouseMove handlers to ListView

wpf-project

In order to simplify the logic of the program, let`s add a class and name it FileObject, that contains Name (image.jpeg, essay.txt etc) and Fullname (C:/somefolder/image.jpeg, E:/myessays/essay.txt etc):

...
    class FileObject
    {
        public string Name { get; set; }

        public string FullName { get; set; }

        public FileObject(string name, string fullname)
        {
            Name = name;
            FullName = fullname;
        }

        public override string ToString()
        {
            return Name;
        }
    } 
...

By means of the following code user is able:

  • to drop some file to the list.
  • to drop some file to some folder from list

Dragging the file to ListView, it is possible to handle drop event and get the full file`s path:

...
     public readonly string DirPath = AppDomain.CurrentDomain.BaseDirectory;      /*path to the folder where the executable file is*/

        private void lv_list_Drop(object sender, DragEventArgs e)
        {
            string[] file_pathes = (string[])e.Data.GetData(DataFormats.FileDrop);  /*file_pathes contains pathes of dragged selected files*/

            //add every file to the list copping them to the application directory
            foreach (var path in file_pathes)
            {
                string new_path = DirPath + Path.GetFileName(path); //New file path

                File.Copy(path, new_path);                         //copping dragged file by new_path

                lv_list.Items.Add(new FileObject(Path.GetFileName(path), new_path));  //adding FileObject (that stores file info) to the list
            }
        }
  
...

Note: ListView can put any object to its items property. To show textual representation of the items ListView calls method ToString() that is marked as virtual in object class. That is why ToString() is overridden in FileObject, to show short file name in the list.

To drag some file from the list is it necessary to select it and put cursor in some folder (or browser window etc) realising the left button of the mouse:

...
        private void lv_list_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.MouseDevice.LeftButton==System.Windows.Input.MouseButtonState.Pressed) //Checking that leftbutton is pressed
            {
                var file = lv_list.SelectedItem as FileObject; //Taking the selected object as FileObject

                if(file!=null) //Check that item is really selected. Alternatively file variable equals null

                DragDrop.DoDragDrop(this, new DataObject(DataFormats.FileDrop, new string[] { file.FullName }), DragDropEffects.Move);/*new string[] {...} - array contains files` pathes user wants to drag from list*/

                lv_list.Items.Remove(file);                   //removing dropped obect from the list
            }
        }
...

Drag & Drop in WinForm .Net Core/Framework

Instructions are almost similar to the previous ones:

  • Create project
  • Choose Control element (ListView, DataGridView, ListBox etc)
  • Add DragDrop and MouseMove handlers
...
    private void YourElementControl_DragDrop(object sender, DragEventArgs e)
    {
       ...
       foreach (string path in (string[])e.Data.GetData(DataFormats.FileDrop))
            {
                File.Copy(path, DirPath + Path.GetFileName(path));
            }
       ...
    }

    private void YourElementControl_MouseMove(object sender, MouseEventArgs e)
    {
     ...
         if (e.Button == MouseButtons.Left)
         {
                 DoDragDrop(new DataObject(DataFormats.FileDrop, new string[] { PathToFirstFile,PathToTheNextOne }), DragDropEffects.Move);
         }
     ...
    }
...

Note: It is possible to implement drag-and-drop for both files and folders. Also it is possible to use various Control elements for implementation.

LEAVE A RESPONSE

Related Posts