.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Working with drag and drop file selection in Windows Form application

Posted By:ASPEvil       Posted Date: July 31, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

Select a file by using Darg & Drop in Windows Form application
 

Sometimes, to provide a better user experience we may need to provide a file selection by Drag&Drop, means, if the user will select a file on his/her computer and drag & drop that file to your Windows Form application, then that file will be selected by your application; you can say it is an alternative to the OpenFileDialog Control which is used to select a particular file by the user, on the user's computer.
This article shows you how you can do this.

Lets see how to make this thing done.

I took a textbox control and made its "multiline" property to true and make its hight a bit bigger and also made its "backcolor" something different than its default white colored background, so that it looks a separate place for some special action.

Now, I made the "AllowDrop" property of this same textbox control to "True" and by doing this, this textbox's "DragDrop" event can work.

Now I wrote the following code with its "DragDrop" event:-
------------------------------------------
[VB.Net code]

Private Sub txt_draganddrop_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles txt_draganddrop.DragDrop
        Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
        If files.Count > 1 Then
            MessageBox.Show("You CAN'T Drag&Drop multiple files at a time.", "You CAN'T Drag&Drop multiple files at a time.", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Exit Sub
        End If

        For Each path In files            
            txt_filelocation.Text = path
        Next

        
        For i As Integer = 0 To files.Count - 1
            Dim file_name As String = Path.GetFileName(files(i))
            txt_filename.Text = file_name
        Next        
    End Sub

------------------------------------------

[C# code]
-----------------
private void txt_draganddrop_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
        string[] files = e.Data.GetData(DataFormats.FileDrop);
        if ((files.Count > 1)) {
            MessageBox.Show("You CAN\'T Drag&Drop multiple files at a time.", "You CAN\'T Drag&Drop multiple files at a time.", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            return;
        }
        foreach (path in files) {
            txt_filelocation.Text = path;
        }
        for (int i = 0; (i 
                    <= (files.Count - 1)); i++) {
            string file_name = Path.GetFileName(files[i]);
            txt_filename.Text = file_name;
        }
    }

-----------------


Here, with the above code, My that textbox(which I have separated with a backcolor and enlarging its height a bit) name is: "txt_draganddrop". With its DragDrop even, I first checked whether multiple files have been selected by the code: "If files.Count > 1 Then", if it gets true, then multiple files have been selected which I am not wanting in my case, so stop the progress by "Exit Sub" in vb.net code Or "Return" in c# code. Though, you may allow multiple file selection, if you want and in that case you do not need to use this part of the code.

Now with the vb.net code: "For Each path In files           
            txt_filelocation.Text = path
        Next
", Or c# code: "foreach (path in files) {
            txt_filelocation.Text = path;
        }
". Here with this part of the code, I put the select file's file path to a different textbox named as:  txt_filelocation

Now into an another textbox control: "txt_filename", I put the selected file's file name, using this vb.net code: "For i As Integer = 0 To files.Count - 1
            Dim file_name As String = Path.GetFileName(files(i))
            txt_filename.Text = file_name
        Next   
" Or c# code: "for (int i = 0; (i
                    <= (files.Count - 1)); i++) {
            string file_name = Path.GetFileName(files[i]);
            txt_filename.Text = file_name;
        }
"

Now, you got the selected file's file location and file name. You can save this file information with the database and save the file with your application, as per your requirement.


Thanks.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend