Results 1 to 4 of 4

Thread: filtering dragenter file in label?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    126

    Question 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...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2009
    Posts
    126

    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
    Last edited by Joke Sparrow; Dec 15th, 2011 at 02:02 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width