filtering dragenter file in label?
Alright i have a code for getting the path of drop file to my label..
Code:
Private Sub Label1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
If e.Data.GetDataPresent("FileDrop") Then
Dim theFiles() As String = CType(e.Data.GetData("FileDrop", True), String())
For Each theFile As String In theFiles
MsgBox(theFile)
Next
End If
End Sub
Private Sub Label1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
how to filter the file that i want to drop to my label?
for example i just want to filter it to "demo.exe" or "*.exe" how can i do this?
or is it possible to code this to drag enter?
huh.... i know this in vb6 but for vb.net..:(
please help me..im new to .net...:(
Re: filtering dragenter file in label?
First up, you are using DataFormats.FileDrop in the DragEnter handler and "FileDrop" in the DragDrop handler. There's no reason to be doing the same thing in two different ways. Pick one and stick to it. The obvious choice is to use the DataFormats enumeration.
Just as you can use GetDataPresent in both DragEnter and DragDrop, so too you can use GetData in both DragEnter and DragDrop. That means that you can get the data in DragEnter, use the IO.Path class to confirm that the name and/or the extension are valid and then allow the Copy effect or not based on that.
Re: filtering dragenter file in label?
thank you jmc...
i found this code..
Code:
Dim Files() As String
Files = e.Data.GetData(DataFormats.FileDrop)
Dim sReader As New StreamReader(Files(0))
' get the filename from the file without the path
Dim file_name As String = Path.GetFileName(Files(0))
' check the extension of the file
If Path.GetExtension(Files(0)) = ".txt" Or _
Path.GetExtension(Files(0)) = ".blabla" Then
' do your thing
Else
' show warning
End if
last question how about getting filename?
edited: sorry for being newbie to vb.net i found out the answer of my question using Path.GetFileName
Re: filtering dragenter file in label?
Note that you should never do anything twice when you can do it once and cache the result. There's no point calling GetExtension twice. Call it once, assign the result to a variable and then use that variable twice.