23/11/2019 19

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 function in C#.

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;
            else
                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")
                {
                    lstYol.Items.Add(music);
                    listMusic.Items.Add(Path.GetFileNameWithoutExtension(music));
                    database.sarki_ekle(music);

                }



                if (Path.GetExtension(music) == ".tml")
                {
                    listMusic.Items.Clear();
                    lstYol.Items.Clear();



                    StreamReader oku = new StreamReader(music, Encoding.UTF8);
                    string msc = oku.ReadLine();
                    while (msc != null)
                    {
                        lstYol.Items.Add(msc);
                        listMusic.Items.Add(Path.GetFileNameWithoutExtension(msc));
                        database.sarki_ekle(msc);
                        msc = oku.ReadLine();
                    }
                    oku.Close();
                }
            }

        }

That is all for this article.

Have a nice Dragging and Dropping.

Burak Hamdi TUFAN


Tags

Share this Post



Post a Comment

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

Comments

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