Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I Drag and Drop a *.cs or *.vb file in a OpenFileDialog
to a textbox, on a tabcontrol, on a form?

Most of the D+D examples that are out there do
not work with Windows 7.

I am using VS2008(VB) w/sp1, .Net 3.5, Windows 7 w/SP1.

I have looked almost everywhere and have not found any
working examples.

THANX in advance for a working example.
Posted
Comments
[no name] 12-May-11 10:24am    
Are you asking to "open" the contents of the *.cs/*.vb file in a textbox, or simply to put the filename selected in the OpenFileDialog into the textbox?
rspercy65 12-May-11 10:29am    
The file has to be opened for editing or reading.

I don't believe you can drag-and-drop FROM the OpenFileDialog component (at least, I haven't had success with it). If I understand your question, I would do the following (assumes a Form1, Button1, TabControl1 (2 TabPages), and Textbox1 (on TabPage1):

VB
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "C# (*.cs)|*.cs|VB (*.vb)|*.vb|All Files (*.*)|*.*"
        OpenFileDialog1.InitialDirectory = "C:\"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.RestoreDirectory = True
        If OpenFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            TextBox1.Text = OpenFileDialog1.FileName
        End If
    End Sub
End Class
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-May-11 23:11pm    
Sanity against lame! My 5.
--SA
rspercy65 12-May-11 23:55pm    
I meant DragDrop from Windows Explorer, not OpenFileDialog. Sorry about the confusion. Your
solution does work, so I gave you a 5
[no name] 13-May-11 8:18am    
Thanks, both of you!
rspercy60, if you're still interested in drag/drop, here's a way to do it from Windows Explorer. I made it quite minimalist -- no error checking for the specified filetype, for instance, but that is somewhat dependent on your environment and usage, and should be easy to add anyway. Hope it helps!

VB
Public Class Form1
    'Assumes: 1) a TabControl1 w/2 tabs, 2) a Textbox1 on 1st tab,
    '         and 3) a RichTextBox1 on 2nd tab.
    'Execution: Drop a text file from windows explorer onto the TextBox1
    '           control.  The filename of the dragged object,
    '           e.Data.GetData(DataFormats.FileDrop)(0), will be inserted
    '           into TextBox1, and will also be used by
    '           My.Computer.FileSystem.ReadAllText to open the text file,
    '           read the contents and place them in RichTextBox1.
    '
    '           If you wish to open the text file for other purposes, you
    '           can replace the My.Computer.FileSystem.ReadAllText with
    '           whatever other method you wish to use, such as
    '           StreamReader/StreamWriter.
    '
    'Notes:     - Be sure the TextBox object has its "AllowDrag" property
    '             set to True.
    '           - This example does not validate that the filetype is
    '             able to be loaded as text into the RichTextBox1.
    Private Sub TextBox1_DragEnter(ByVal sender As System.Object, _
                                   ByVal e As System.Windows.Forms.DragEventArgs) _
                                   Handles TextBox1.DragEnter
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub TextBox1_DragDrop(ByVal sender As System.Object, _
                                  ByVal e As System.Windows.Forms.DragEventArgs) _
                                  Handles TextBox1.DragDrop
        TextBox1.Text = e.Data.GetData(DataFormats.FileDrop)(0)
        RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(TextBox1.Text)
        TabControl1.SelectTab(1)
    End Sub
End Class
 
Share this answer
 
Comments
rspercy65 13-May-11 11:32am    
This is not working for me. I tried this and several other routines and none of them worked either. I am dragging a (.cs|.vb) file onto a textbox named tb. Here is the code...

Private Sub ProcessFile(ByVal fileName As String)
If fileName <> Nothing Then
If fileName.EndsWith(".cs") Then
Dim popupMenu As New AutocompleteMenu(tb)
popupMenu.Items.ImageList = iLAutocomplete
tb.Language = Language.CSharp
BuildAutocompleteMenu(popupMenu)
tb.Text = My.Computer.FileSystem.ReadAllText(fileName)
Else
Dim popupMenu As New AutocompleteMenu(tb)
popupMenu.Items.ImageList = iLAutocomplete
tb.Language = Language.VB
BuildAutocompleteMenuVB(popupMenu)
tb.Text = My.Computer.FileSystem.ReadAllText(fileName)
End If
End If
Thread.Sleep(1000)

End Sub

Private Sub tb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tb.DragDrop

Dim file_path As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop, False), String())
Dim file As String = file_path(0)

Dim proc As ProcessFileDelegate = AddressOf ProcessFile
proc.BeginInvoke(file, Nothing, Nothing)

End Sub

Private Sub tb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tb.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

I am using a Delegate Sub so the Windows Explorer and My App do not get hung-up.
[no name] 13-May-11 14:28pm    
In tb_DragDrop, if you set a breakpoint at "Dim proc As ProcessFileDelegate", is the value of the file variable correct (i.e., the name of the dragged file)?
[no name] 13-May-11 15:20pm    
I just noticed that in your ProcessFile sub, tb.Text is being set to "ReadAllText(fileName)". Assuming tb is a textbox, don't you want to put just the filename itself into the textbox, rather than the entire file contents? I.e., tb.Text = fileName. Or perhaps put the contents into an object designed for paragraphs of text, like a RichTextBox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900