-
Nov 14th, 2018, 11:51 AM
#1
Thread Starter
Junior Member
[RESOLVED] Drag and Drop Problem
I have a drag and drop problem I cannot find a solution for. I want to allow the user to drop a text file onto a richtextbox but its being blocked somehow. I have VS2017 and Windows 10. UAC=NONE and I am the Admin of the PC. AllowDrop=True on the Form properties and EnableAutoDragDrop=True is on the Richtextbox properties.
I also tried adding
Me.AllowDrop = True
Me.RichTextBox1.AllowDrop = True
to the Form1_Load but that had no effect.
When I drag a text file over the form as soon as it reaches the form the cursor changes to a "NOT" symbol and if I drop the file the DragDrop event doesn't fire but the DragEnter event does fire when the drag reaches the richtextbox control. But the DragEnter event always goes to the ELSE stament even if the file is a text file. Its as if some security is blocking VB from accessing the file. 2 days of searching for a solution and I found nothing helpful. Does anyone know what am I missing. What is blocking the drop event?
Thanks for any help.
Code:
Private Sub RichTextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragDrop
Dim filenames As String() = TryCast(e.Data.GetData(DataFormats.FileDrop), String())
Dim filetype As String = filenames(0).Substring(filenames(0).LastIndexOf("\") + 1)
If filetype.IndexOf(".txt") <> -1 Then
RichTextBox1.LoadFile(filenames(0), RichTextBoxStreamType.PlainText)
ElseIf filetype.IndexOf(".rtf") <> -1 Then
RichTextBox1.LoadFile(filenames(0), RichTextBoxStreamType.RichText)
Else
MessageBox.Show("Sorry, I cannot support this file type")
End If
End Sub
Private Sub RichTextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
-
Nov 14th, 2018, 06:56 PM
#2
Re: Drag and Drop Problem
If execution passes to the Else block in that DragEnter event handler then clearly the Text format is not present. If you're dragging a file then that's not surprising. The thing to do is to find out what formats the data is available in and choose a suitable one, then handle that appropriately. It's likely to be FileDrop, which I think will provide the path of the file. That's logical because the system has no idea whether the file being dropped contains text or, if it does, what the encoding might be. Even if it did, it doesn't know that your app actually wants the text rather than the path. It's up to you to decide how to process the dropped file.
I suggest that you read the following thread:
www.vbforums.com/showthread.php?546261
Be sure to read the first three posts at least.
-
Nov 15th, 2018, 09:46 AM
#3
Thread Starter
Junior Member
Re: Drag and Drop Problem
Thanks that helps a lot. Not sure why your suggested post didn't come up in my searches, maybe I didn't read back far enough since its from 2008. Using that information I have identified the problems with my code.
1. The DragEnter routine is what is blocking my drop. If e.effect is set to None it blocks the drop so first I need to fix my DragEnter so it properly detects if the file is text or rtf and it choses the correct effect. I set both the IF and ELSE in DragEnter to Copy as a test. When I do this it allows me to Drop the file. In all the docs and sample code I reviewed this was not made clear until the post you provided.
2. Now when I drop the file its dropping the files icon into the RichTextBox and not loading the text. So my 2nd problem to solve is getting the DragDrop to load the contents of the file and not the icon.
I am going to go back to your post and read it in detail. Thanks for the push in the right direction.
-
Nov 15th, 2018, 10:30 AM
#4
Thread Starter
Junior Member
Re: Drag and Drop Problem
Ok I have worked out my 2 remaining issues. To stop the icon from getting dropped into the RichTextBox I had to turn off "EnableAutoDragDrop" on the RichTextBox properties.
The following code seems to function the way I want.
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.RichTextBox1.AllowDrop = True
End Sub
Private Sub RichTextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragDrop
Dim filenames As String() = TryCast(e.Data.GetData(DataFormats.FileDrop), String())
Dim filetype As String = filenames(0).Substring(filenames(0).LastIndexOf("\") + 1)
If filetype.IndexOf(".txt") <> -1 Then
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(filenames(0))
ElseIf filetype.IndexOf(".rtf") <> -1 Then
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(filenames(0))
Else
MessageBox.Show("Must be a Text or RichText file!")
End If
End Sub
Private Sub RichTextBox1_DragEnter(sender As Object, e As DragEventArgs) Handles RichTextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
End If
End Sub
-
Nov 15th, 2018, 06:09 PM
#5
Re: [RESOLVED] Drag and Drop Problem
Just one point I would mention there is that this code is wrong:
vb.net Code:
Dim filenames As String() = TryCast(e.Data.GetData(DataFormats.FileDrop), String()) Dim filetype As String = filenames(0).Substring(filenames(0).LastIndexOf("") + 1)
The whole point of TryCast is to attempt a cast that might fail without throwing an exception so you should never assume that TryCast produces a result. TryCast should ALWAYS be followed by a null check because it will produce Nothing if the cast failed:
vb.net Code:
Dim filenames As String() = TryCast(e.Data.GetData(DataFormats.FileDrop), String()) If filenames IsNot Nothing Then Dim filetype As String = filenames(0).Substring(filenames(0).LastIndexOf("") + 1)
If you know for a fact that the cast will not fail then you should not be using TryCast at all, but rather DirectCast:
vb.net Code:
Dim filenames As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()) Dim filetype As String = filenames(0).Substring(filenames(0).LastIndexOf("") + 1)
-
Nov 15th, 2018, 06:18 PM
#6
Re: [RESOLVED] Drag and Drop Problem
Two other points:
1. Use the System.IO.Path class for manipulating folder and file paths. Path.GetExtension will give you the file extension from a file name or path.
2. The RichTextBox has its own LoadFile method that you should use to load a file. You can specify whether to read the file as plain text or as RTF. In your case, I think that assigning the contents of an RTF file to the Text property would actually break it. At the very least, you should be assigning RTF markup to the Rtf property. If you have a file though, just call LoadFile.
-
Nov 15th, 2018, 06:21 PM
#7
Re: [RESOLVED] Drag and Drop Problem
A final point:
You might not want to do this because it means no specific feedback but, if the user can only drop certain types of files, I would tend to do the file type check in the DragEnter event handler and disallow the drag. If you're going to allow the drop, should you be limiting them to just TXT and RTF files? There are other file extensions that can be used for plain text files, e.g. CSV or TAB.
Tags for this Thread
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
|