Drag and drop files from the Desktop (or Windows Explorer) to RadListView

3 posts, 0 answers
  1. Alejandro
    Alejandro avatar
    2 posts
    Member since:
    Nov 2013

    Posted 14 Jan 2014 Link to this post

    Hello guys, I hope you are doing great.

    I'm working on a WinForms project. One of the requirements is to drag files from the Desktop (or Windows Explorer) and drop them on a RadListView component. 

    I set the AllowDrop and AllowDragDrop properties of the my listView object, and then I added the following events:

    private void listView_DragEnter(object sender, DragEventArgs e)
    {
        MessageBox.Show("DragEnter");
    }
     
    private void listView_DragDrop(object sender, DragEventArgs e)
    {
        MessageBox.Show("DragDrop");
    }

    When I drag an item from the Desktop and enter the listView area, I get the "DragEnter" message. If I comment that code out, and then drop an item on the listView, I don't get the "DragDrop" message. 

    My first question is: is it possible to achieve what I'm trying to do? And if it is possible, my second question is: what am I doing wrong?

    I created a small test project to isolate the situation. The following is the complete code of the form class:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
     
    namespace DragFromDesktop
    {
        public partial class DragFromDesktopForm : Telerik.WinControls.UI.RadForm
        {
            public DragFromDesktopForm()
            {
                InitializeComponent();
            }
     
            private void DragFromDesktopForm_Load(object sender, EventArgs e)
            {
     
            }
     
            private void closeButton_Click(object sender, EventArgs e)
            {
                this.Close();
            }
     
            private void listView_DragEnter(object sender, DragEventArgs e)
            {
                //MessageBox.Show("DragEnter");
            }
     
            private void listView_DragDrop(object sender, DragEventArgs e)
            {
                MessageBox.Show("DragDrop");
            }
        }
    }

    I'm also attaching an example of how the form looks like. The big white square is the list view which I want to drop the items to.

    Thanks in advance.

    Best regards,

    Alejandro
  2. Alejandro
    Alejandro avatar
    2 posts
    Member since:
    Nov 2013

    Posted 16 Jan 2014 Link to this post

    I finally made it work using something like this:

    /// <summary>
    /// Event triggered when an item begins to be dragged on the list.
    /// </summary>
    /// <param name="sender">The control which the action is for.</param>
    /// <param name="e">The event data.</param>
    private void listView_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Copy;
        }
    }
     
    /// <summary>
    /// Event triggered when the drag and drop operation ends.
    /// </summary>
    /// <param name="sender">The control which the action is for.</param>
    /// <param name="e">The event data.</param>
    private void listView_DragDrop(object sender, DragEventArgs e)
    {
        string[] handles = (string[])e.Data.GetData(DataFormats.FileDrop, false);
     
        foreach (string path in handles)
        {
            if (File.Exists(path))
            {
                // TODO: add the file to the list.
            }
            else if (Directory.Exists(path))
            {
                DirectoryInfo info = new DirectoryInfo(path);
                FileInfo[] files = info.GetFiles();
                foreach (FileInfo file in files)
                {
                    // TODO: add the file to the list.
                }
            }
        }
    }
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 17 Jan 2014 Link to this post

    Hello Alejandro,

    Thank you for contacting Telerik Support.

    I am glad that the issue you were facing is now resolved. Performing Drag-and-Drop Operations in Windows Forms article may be quite useful about accomplishing drag-and-drop operations within Windows applications.

    Please do not hesitate to contact us if you have any additional questions.

    Regards,
    Desislava
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top