Drag and Drop in C#

Drag and Drop in C#

Hello everyone, in this article we are going to talk about how can we enable and use Drag and Drop specification in C#. We are going to make an example on Windows Forms.

Let's get started...

Thanks to this property of programs we can hold the relatid file and use drag-drop specification. Now we can use this specification in C#.net applications. First we need to aneble the AllowDrop of form or which component we want to use for drag and drop. Below image shows how to enable from properties section of Visual Studio.

Firstly select the component to enable and set as True below marked property :
Visual Studio AllowDrop - Setting

After enabling the Allow Drop property of the our object, now we have to build the events when the file dragged and dropped. We are going to use DragEnter and DragDrop events of the object. To do this select the related object and open the events tab on the visual studio. And then create and determine the functions of these two events.

Below image has an example about this:
DragEnter and DragDrop Events
And also make sure the code blocks added into the source code of the form:
DragEnter and DragDrop Code Blocks

Below code block is a preperation for a drop process. We are getting ready a list which has all list of files that dragged into the related area. We are allowing all files to drop according to below code block in C#. When drop event fired we can fetch the all dropped files list due to below code below.

        private void list_DragEnter(object sender, DragEventArgs e)
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.All;
                e.Effect = DragDropEffects.None;

Below code block is the function of what we want to use with the dropped files. I prefer the check files for MP3 format and the a specified playlist format specified by me. If the file is MP3 add it to the list and also database of music list. If the format is equal with the my specified format, read all list and add all list to the database and the music list. You can do whatever you want with thisproperty.

 private void list_DragDrop(object sender, DragEventArgs e)
            string[] dosyalar = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            StreamWriter yaz;
            foreach (string music in dosyalar)
                if (Path.GetExtension(music) == ".mp3")

                if (Path.GetExtension(music) == ".tml")

                    StreamReader oku = new StreamReader(music, Encoding.UTF8);
                    string msc = oku.ReadLine();
                    while (msc != null)
                        msc = oku.ReadLine();

That is all for this article.

Have a nice Dragging and Dropping.

Burak Hamdi TUFAN


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!


  • There is no comment. Be the owner of first comment...