Results 1 to 6 of 6

Thread: [RESOLVED] drag and load a file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    187

    Resolved [RESOLVED] drag and load a file

    hi I want to drag a file over a richtextbox, or a form and then I want that this file is loaded automatically in richtextbox....how can i do??

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: drag and load a file

    The richtextbox must know the file type in order to open it correctly. The following code gives you also the file path to the dragged file so you can start from there if you find out, that the default LoadFile is not working for you.
    First set the AllowDrop property of both the form and the RichTextBox to true.
    Code for the form:
    VB Code:
    1. Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
    2.  
    3.         'display the copy cursor if the data is a file
    4.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    5.             e.Effect = DragDropEffects.Copy
    6.         Else
    7.             e.Effect = DragDropEffects.None
    8.         End If
    9.  
    10.     End Sub
    11.  
    12.     Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
    13.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    14.             'put the file names to a string array in case the user has selected multiple files.
    15.             Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
    16.             Dim FilePath As String 'this is the path to the dragged file
    17.  
    18.             Try
    19.                 FilePath = files(0)
    20.                 Me.Activate() 'to bring the app to front
    21.                 RichTextBox1.LoadFile(FilePath)
    22.  
    23.             Catch ex As Exception
    24.                 MessageBox.Show(ex.Message)
    25.                 Return
    26.             End Try
    27.         End If
    28.     End Sub
    Code for the RichTextBox:
    VB Code:
    1. Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
    2.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    3.             e.Effect = DragDropEffects.Copy
    4.         Else
    5.             e.Effect = DragDropEffects.None
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
    10.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    11.             'put the file names to a string array in case the user has selected multiple files.
    12.             Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
    13.             Dim FilePath As String 'this is the path to the dragged file
    14.  
    15.             Try
    16.                 FilePath = files(0)
    17.                 Me.Activate() 'to bring the app to front
    18.                 RichTextBox1.LoadFile(FilePath)
    19.  
    20.             Catch ex As Exception
    21.                 MessageBox.Show(ex.Message)
    22.                 Return
    23.             End Try
    24.         End If
    25.     End Sub

    --------------------
    EDIT: the above will work only with .rtf files. If you plan on opening .txt, change "RichTextBox1.LoadFile(FilePath)" to
    "RichTextBox1.LoadFile(FilePath, RichTextBoxStreamType.PlainText)" but then it will stop showing RTFs correctly.
    Last edited by Half; Nov 30th, 2006 at 06:52 AM.
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    187

    Re: drag and load a file

    many thanks....very good!!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    187

    Re: [RESOLVED] drag and load a file

    sorry, I've got a problem, when i drag the file on a richtetxbox, appear the text of the file but also the icon of the file....like if it is copied in richtextbox.
    why?
    how can i do if I want visualize only text without the icon file??

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [RESOLVED] drag and load a file

    What is your exact code? The unneeded data should be filtered out.
    VB 2005, Win Xp Pro sp2

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Mar 2006
    Posts
    187

    Re: [RESOLVED] drag and load a file

    the same that you have posted.....idem...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width