Drap and Drop Question

  • Thread starter Thread starter zacks
  • Start date Start date
Z

zacks

I am trying to implement a form that has a TextBox control where the
user will key in a SQL Script. I would like to have the user be able
to drag a pre-existing query into it by dragging the file from a
Windows Explorer window into it. I can get the Drag Effect to work as
long as I key on the FileDrop DataFormat. But this does not take into
account the content of the file. It allows any type of file to be
dropped. The StringFormat or Text format do not appear applicable. I
would prefer to not have to handle different file types in the
DragDrop event by not even allowing it for non-Text files.

What is the best way to do this?
 
There is no such thing as a text or non-text file. The file is a stream
of bytes which can be interpreted in various ways, sometimes text, sometimes
not. The best you can do is filter on extension, or try and change the
bytes into a string through the encoding classes and work from there.
 
    There is no such thing as a text or non-text file.  The file is a stream
of bytes which can be interpreted in various ways, sometimes text, sometimes
not.  The best you can do is filter on extension, or try and change the
bytes into a string through the encoding classes and work from there.

I was hoping there was some kind of built in way to handle this, but I
handled it with the following code:

Private Sub txtQuery_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles txtQuery.DragEnter

Dim fi As FileInfo
Dim sFiles As String()

If e.Data.GetDataPresent(DataFormats.FileDrop) Then
sFiles = CType(e.Data.GetData(DataFormats.FileDrop),
String())
fi = New FileInfo(sFiles(0))
If fi.Extension.ToUpper = ".SQL" Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
Else
e.Effect = DragDropEffects.None
End If

End Sub

A more generic way to do it would be to set the Tag property of the
control to the file extension the control accepts and reference the
sender's Tag property. That way a single DragEnter event handler could
handle multple controls on the same form.
 
Good morning,

Please pardon my meddling, but consider this:

Private Sub txtQuery_DragEnter( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles txtQuery.DragEnter

e.Effect = DragDropEffects.None
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim sFiles As String() = _
CType(e.Data.GetData(DataFormats.FileDrop), String())

If sFiles(0).ToUpper.EndsWith(".SQL") Then
e.Effect = DragDropEffects.Copy
End If
End If

End Sub


RappyFood

Please be advised...
I haven't had my second cup of coffee yet.

There is no such thing as a text or non-text file. The file is a stream
of bytes which can be interpreted in various ways, sometimes text,
sometimes
not. The best you can do is filter on extension, or try and change the
bytes into a string through the encoding classes and work from there.

I was hoping there was some kind of built in way to handle this, but I
handled it with the following code:

Private Sub txtQuery_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles txtQuery.DragEnter

Dim fi As FileInfo
Dim sFiles As String()

If e.Data.GetDataPresent(DataFormats.FileDrop) Then
sFiles = CType(e.Data.GetData(DataFormats.FileDrop),
String())
fi = New FileInfo(sFiles(0))
If fi.Extension.ToUpper = ".SQL" Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
Else
e.Effect = DragDropEffects.None
End If

End Sub

A more generic way to do it would be to set the Tag property of the
control to the file extension the control accepts and reference the
sender's Tag property. That way a single DragEnter event handler could
handle multple controls on the same form.
 
Nicholas said:
There is no such thing as a text or non-text file.

Well, it depends. For us humans, there is a difference.

If a file only contains printable characters, one can very likely
consider it a text file. To determine if a file is text, I usually read
the first 1024 bytes and determine if they are all non-control
characters. Of course I look for special Unicode markers at the
beginning too, and act accordingly (e.g. try to interpret the bytes as
Unicode).
 
Back
Top