VB.Net by API/System.Windows.Forms/DragDropEffects
DragDropEffects.Copy
Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.IO
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim myform As New Form1()
Application.Run(myform)
End Sub
End Class
Public Class Form1
" Allow Copy if there is FileDrop data.
Private Sub lblDropTarget_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
" Display the dropped file names.
Private Sub lblDropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragDrop
lstFiles.Items.Clear()
Dim file_names As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each file_name As String In file_names
lstFiles.Items.Add(file_name)
Next file_name
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.lstFiles = New System.Windows.Forms.ListBox
Me.lblDropTarget = New System.Windows.Forms.Label
Me.SuspendLayout()
"
"lstFiles
"
Me.lstFiles.Dock = System.Windows.Forms.DockStyle.Fill
Me.lstFiles.FormattingEnabled = True
Me.lstFiles.Location = New System.Drawing.Point(0, 48)
Me.lstFiles.Name = "lstFiles"
Me.lstFiles.Size = New System.Drawing.Size(274, 160)
Me.lstFiles.TabIndex = 3
"
"lblDropTarget
"
Me.lblDropTarget.AllowDrop = True
Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblDropTarget.Dock = System.Windows.Forms.DockStyle.Top
Me.lblDropTarget.Location = New System.Drawing.Point(0, 0)
Me.lblDropTarget.Name = "lblDropTarget"
Me.lblDropTarget.Size = New System.Drawing.Size(274, 48)
Me.lblDropTarget.TabIndex = 2
Me.lblDropTarget.Text = "Drop Target"
Me.lblDropTarget.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(274, 210)
Me.Controls.Add(Me.lstFiles)
Me.Controls.Add(Me.lblDropTarget)
Me.Name = "Form1"
Me.Text = "AcceptFiles"
Me.ResumeLayout(False)
End Sub
Friend WithEvents lstFiles As System.Windows.Forms.ListBox
Friend WithEvents lblDropTarget As System.Windows.Forms.Label
End Class
DragDropEffects.Move
Imports System
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms
public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents DestinationTextBox As System.Windows.Forms.TextBox
Friend WithEvents SourceTextBox As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DestinationTextBox = New System.Windows.Forms.TextBox()
Me.SourceTextBox = New System.Windows.Forms.TextBox()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.TreeView1 = New System.Windows.Forms.TreeView()
Me.SuspendLayout()
"
"DestinationTextBox
"
Me.DestinationTextBox.AllowDrop = True
Me.DestinationTextBox.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.DestinationTextBox.Location = New System.Drawing.Point(4, 166)
Me.DestinationTextBox.Multiline = True
Me.DestinationTextBox.Name = "DestinationTextBox"
Me.DestinationTextBox.Size = New System.Drawing.Size(354, 195)
Me.DestinationTextBox.TabIndex = 1
Me.DestinationTextBox.Text = ""
"
"SourceTextBox
"
Me.SourceTextBox.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.SourceTextBox.Location = New System.Drawing.Point(4, 4)
Me.SourceTextBox.Multiline = True
Me.SourceTextBox.Name = "SourceTextBox"
Me.SourceTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.SourceTextBox.Size = New System.Drawing.Size(354, 144)
Me.SourceTextBox.TabIndex = 2
Me.SourceTextBox.Text = "Use your right or left key to drag and drop item in left ListBox or Tree to the bottom TextBox."
"
"ListBox1
"
Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.ListBox1.ItemHeight = 14
Me.ListBox1.Items.AddRange(New Object() {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12", "Item 13", "Item 14", "Item 15"})
Me.ListBox1.Location = New System.Drawing.Point(370, 4)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
Me.ListBox1.Size = New System.Drawing.Size(190, 144)
Me.ListBox1.TabIndex = 3
"
"TreeView1
"
Me.TreeView1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(370, 166)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node0", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node1"), New System.Windows.Forms.TreeNode("Node12", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node13")}), New System.Windows.Forms.TreeNode("Node14"), New System.Windows.Forms.TreeNode("Node15", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node16"), New System.Windows.Forms.TreeNode("Node17")})}), New System.Windows.Forms.TreeNode("Node2", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node3", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node4"), New System.Windows.Forms.TreeNode("Node5"), New System.Windows.Forms.TreeNode("Node6")}), New System.Windows.Forms.TreeNode("Node7"), New System.Windows.Forms.TreeNode("Node8", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node10"), New System.Windows.Forms.TreeNode("Node11")}), New System.Windows.Forms.TreeNode("Node9")})})
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(187, 195)
Me.TreeView1.TabIndex = 4
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.AutoScroll = True
Me.ClientSize = New System.Drawing.Size(564, 367)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TreeView1, Me.ListBox1, Me.SourceTextBox, Me.DestinationTextBox})
Me.Name = "Form1"
Me.Text = "Drag and Drop Demo"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Destination_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DestinationTextBox.DragDrop
DestinationTextBox.Text = e.Data.GetData(DataFormats.Text).ToString
DestinationTextBox.BackColor = Color.White
End Sub
Private Sub Destination_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DestinationTextBox.DragEnter
DestinationTextBox.BackColor = Color.Yellow
e.Effect = DragDropEffects.Copy
End Sub
Private Sub SourceTextBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SourceTextBox.MouseDown
SourceTextBox.DoDragDrop(SourceTextBox.SelectedText, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
Dim i As Integer, selItems As String
For i = 0 To ListBox1.SelectedItems.Count - 1
selItems = selItems & ListBox1.SelectedItems.Item(i) & vbCrLf
Next
SourceTextBox.DoDragDrop(selItems, DragDropEffects.Copy Or DragDropEffects.Move)
End Sub
Private Sub Destination_DragLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationTextBox.DragLeave
DestinationTextBox.BackColor = Color.White
End Sub
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
If e.Button = MouseButtons.Right Then
TreeView1.DoDragDrop(TreeView1.SelectedNode.Text, DragDropEffects.Copy Or DragDropEffects.Move)
End If
End Sub
End Class