Visual Basic .NET: Difference between revisions

m
added drag and drop to code snippets
m (added OniSplitConvert function to Oni Class (code snippet))
m (added drag and drop to code snippets)
Line 19: Line 19:




'''Class Oni'''
====Oni Class====


Functions in this class:
Functions in this class:
Line 132: Line 132:




====Drag and drop====
The target element must have its "AllowDrop" property set to "true".


In the following there's a textbox named "tbXml1". In order to allow only certain file types to be dropped, the code checks the file extension. If the extension is allowed during drag, the user will see a plus sign.
    Private _OniTypes As New HashSet(Of String)(".xml .oni".Split)
    Private Sub tbXml1_DragEnter(sender As Object, e As DragEventArgs) Handles tbXml1.DragEnter
        If Not e.Data.GetDataPresent(DataFormats.FileDrop) Then Return
        For Each s In DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
            If _OniTypes.Contains(Path.GetExtension(s)) Then
                e.Effect = DragDropEffects.Copy
                Return
            End If
        Next
    End Sub
    Private Sub tbXml1_DragDrop(sender As Object, e As DragEventArgs) Handles tbXml1.DragDrop
        ' how many files were dropped at same time ?
        ' MsgBox(e.Data.GetData(DataFormats.FileDrop).length)
        For Each s In DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
            If _OniTypes.Contains(Path.GetExtension(s)) Then
                tbXml1.Text = Path.GetFullPath(s)
            End If
        Next
    End Sub




8,306

edits