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??
Printable View
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??
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:Code for the RichTextBox: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 SubVB 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.
many thanks....very good!!
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??
What is your exact code? The unneeded data should be filtered out.
the same that you have posted.....idem...