|
-
Nov 30th, 2006, 06:09 AM
#1
Thread Starter
Addicted Member
[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??
-
Nov 30th, 2006, 06:43 AM
#2
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:
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
'display the copy cursor if the data is a file
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'put the file names to a string array in case the user has selected multiple files.
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Dim FilePath As String 'this is the path to the dragged file
Try
FilePath = files(0)
Me.Activate() 'to bring the app to front
RichTextBox1.LoadFile(FilePath)
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
End Sub
Code for the RichTextBox:
VB Code:
Private Sub RichTextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub RichTextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles RichTextBox1.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'put the file names to a string array in case the user has selected multiple files.
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Dim FilePath As String 'this is the path to the dragged file
Try
FilePath = files(0)
Me.Activate() 'to bring the app to front
RichTextBox1.LoadFile(FilePath)
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If
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
-
Dec 2nd, 2006, 05:44 AM
#3
Thread Starter
Addicted Member
Re: drag and load a file
many thanks....very good!!
-
Dec 2nd, 2006, 06:15 AM
#4
Thread Starter
Addicted Member
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??
-
Dec 2nd, 2006, 10:30 AM
#5
Re: [RESOLVED] drag and load a file
What is your exact code? The unneeded data should be filtered out.
-
Dec 3rd, 2006, 03:26 AM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|