VB.Net/GUI/Drag Drop
Содержание
Drag and Drop between Labels
<source lang="vbnet"> Imports System Imports System.Runtime.InteropServices Imports System.Drawing Imports System.ruponentModel Imports System.Windows.Forms Imports System.IO Imports System.Xml.Serialization 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
" Start a drag. Private Sub lblDragSource_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles lblDragSource.MouseDown lblDragSource.DoDragDrop("Data!", DragDropEffects.Copy) End Sub " Make sure the drag is coming from lblDragSource. Private Sub lblDropTarget_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles lblDropTarget.DragEnter e.Effect = DragDropEffects.Copy End Sub " Display the dropped data. Private Sub lblDropTarget_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles lblDropTarget.DragDrop MessageBox.Show(e.Data.GetData("Text").ToString) 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.lblDragSource = New System.Windows.Forms.Label Me.lblDropTarget = New System.Windows.Forms.Label Me.SuspendLayout() " "lblDragSource " Me.lblDragSource.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDragSource.Location = New System.Drawing.Point(16, 16) Me.lblDragSource.Name = "lblDragSource" Me.lblDragSource.Size = New System.Drawing.Size(96, 64) Me.lblDragSource.TabIndex = 0 Me.lblDragSource.Text = "Drag Source" Me.lblDragSource.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lblDropTarget " Me.lblDropTarget.AllowDrop = True Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDropTarget.Location = New System.Drawing.Point(168, 16) Me.lblDropTarget.Name = "lblDropTarget" Me.lblDropTarget.Size = New System.Drawing.Size(96, 64) Me.lblDropTarget.TabIndex = 1 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(279, 95) Me.Controls.Add(Me.lblDropTarget) Me.Controls.Add(Me.lblDragSource) Me.Name = "Form1" Me.Text = "LabelDrag" Me.ResumeLayout(False) End Sub Friend WithEvents lblDragSource As System.Windows.Forms.Label Friend WithEvents lblDropTarget As System.Windows.Forms.Label
End Class
</source>
Drag and drop between Two RichTextBox
<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Imports System.Xml.Serialization Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class Public Class Form1
Private Sub frmDragRichText_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load " Give the RichTextBox some text. Dim txt As String = "www.vbex.ru" rchSource.Text = txt rchSource.Select(0, 0) End Sub " Start a drag. Private Sub lblDragSource_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblDragSource.MouseDown " Make a DataObject. Dim data_object As New DataObject data_object.SetData(DataFormats.Text, rchSource.Text) lblDragSource.DoDragDrop(data_object, DragDropEffects.Copy) End Sub " Allow drop of Rtf, Text, and HTML. Private Sub lblDropTarget_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragEnter If e.Data.GetDataPresent(DataFormats.Text) Then e.Effect = DragDropEffects.Copy End If End Sub " Display whatever data we can. Private Sub lblDropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragDrop If e.Data.GetDataPresent(DataFormats.Text) Then lblTarget.Text = e.Data.GetData(DataFormats.Text).ToString Else lblTarget.Text = "" End If 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.lblTarget = New System.Windows.Forms.Label Me.lblDropTarget = New System.Windows.Forms.Label Me.lblDragSource = New System.Windows.Forms.Label Me.rchSource = New System.Windows.Forms.RichTextBox Me.SuspendLayout() " "lblTarget " Me.lblTarget.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTarget.Location = New System.Drawing.Point(0, 232) Me.lblTarget.Margin = New System.Windows.Forms.Padding(3, 1, 3, 2) Me.lblTarget.Name = "lblTarget" Me.lblTarget.Size = New System.Drawing.Size(296, 28) Me.lblTarget.TabIndex = 11 " "lblDropTarget " Me.lblDropTarget.AllowDrop = True Me.lblDropTarget.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDropTarget.Location = New System.Drawing.Point(200, 32) Me.lblDropTarget.Name = "lblDropTarget" Me.lblDropTarget.Size = New System.Drawing.Size(96, 48) Me.lblDropTarget.TabIndex = 9 Me.lblDropTarget.Text = "Drop Target" Me.lblDropTarget.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lblDragSource " Me.lblDragSource.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDragSource.Location = New System.Drawing.Point(0, 32) Me.lblDragSource.Name = "lblDragSource" Me.lblDragSource.Size = New System.Drawing.Size(96, 48) Me.lblDragSource.TabIndex = 8 Me.lblDragSource.Text = "Drag Source" Me.lblDragSource.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "rchSource " Me.rchSource.AllowDrop = True Me.rchSource.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.rchSource.Location = New System.Drawing.Point(0, 0) Me.rchSource.Name = "rchSource" Me.rchSource.Size = New System.Drawing.Size(296, 28) Me.rchSource.TabIndex = 7 Me.rchSource.Text = "" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(296, 345) Me.Controls.Add(Me.lblTarget) Me.Controls.Add(Me.lblDropTarget) Me.Controls.Add(Me.lblDragSource) Me.Controls.Add(Me.rchSource) Me.Name = "Form1" Me.Text = "DragRichText" Me.ResumeLayout(False) End Sub Friend WithEvents lblTarget As System.Windows.Forms.Label Friend WithEvents lblDropTarget As System.Windows.Forms.Label Friend WithEvents lblDragSource As System.Windows.Forms.Label Friend WithEvents rchSource As System.Windows.Forms.RichTextBox
End Class
</source>
Drag and drop serialized Object base on XML
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Xml.Serialization Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1)
End Sub
End Class
Public Class Form1
<Serializable()> _ Public Class Student Public FirstName As String Public LastName As String Public Sub New() End Sub Public Sub New(ByVal first_name As String, ByVal last_name As String) FirstName = first_name LastName = last_name End Sub End Class Private Sub lblDragSource_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblDragSource.MouseDown Dim emp As New Student(txtFirstName.Text, txtLastName.Text) Dim data_object As New DataObject() data_object.SetData("Student", emp) If lblDragSource.DoDragDrop(data_object, _ DragDropEffects.Copy Or DragDropEffects.Move) = DragDropEffects.Move _ Then txtFirstName.Text = "" txtLastName.Text = "" End If End Sub Private Sub lblDropTarget_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragOver If e.Data.GetDataPresent("Student") Then Const KEY_CTRL As Integer = 8 If (e.KeyState And KEY_CTRL) <> 0 Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If End If End Sub Private Sub lblDropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragDrop Dim emp As Student = DirectCast(e.Data.GetData("Student"), Student) lblFirstName.Text = emp.FirstName lblLastName.Text = emp.LastName End Sub Private Sub btnShowSerialization_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowSerialization.Click Dim emp As New Student(txtFirstName.Text, txtLastName.Text) Dim string_writer As New StringWriter() Dim serializer As New XmlSerializer(GetType(Student)) serializer.Serialize(string_writer, emp) MessageBox.Show(string_writer.ToString, _ "Serialization", MessageBoxButtons.OK, _ MessageBoxIcon.Information) 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.lblLastName = New System.Windows.Forms.Label Me.lblFirstName = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.btnShowSerialization = New System.Windows.Forms.Button Me.txtLastName = New System.Windows.Forms.TextBox Me.txtFirstName = New System.Windows.Forms.TextBox Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.lblDropTarget = New System.Windows.Forms.Label Me.lblDragSource = New System.Windows.Forms.Label Me.SuspendLayout() " "lblLastName " Me.lblLastName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblLastName.Location = New System.Drawing.Point(288, 104) Me.lblLastName.Name = "lblLastName" Me.lblLastName.Size = New System.Drawing.Size(136, 20) Me.lblLastName.TabIndex = 28 Me.lblLastName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft " "lblFirstName " Me.lblFirstName.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblFirstName.Location = New System.Drawing.Point(288, 80) Me.lblFirstName.Name = "lblFirstName" Me.lblFirstName.Size = New System.Drawing.Size(136, 20) Me.lblFirstName.TabIndex = 27 Me.lblFirstName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft " "Label3 " Me.Label3.AutoSize = True Me.Label3.Location = New System.Drawing.Point(224, 104) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(54, 13) Me.Label3.TabIndex = 26 Me.Label3.Text = "Last Name" " "Label4 " Me.Label4.AutoSize = True Me.Label4.Location = New System.Drawing.Point(224, 80) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(53, 13) Me.Label4.TabIndex = 25 Me.Label4.Text = "First Name" " "btnShowSerialization " Me.btnShowSerialization.Location = New System.Drawing.Point(56, 136) Me.btnShowSerialization.Name = "btnShowSerialization" Me.btnShowSerialization.Size = New System.Drawing.Size(200, 24) Me.btnShowSerialization.TabIndex = 24 Me.btnShowSerialization.Text = "Show Serialization" " "txtLastName " Me.txtLastName.Location = New System.Drawing.Point(72, 104) Me.txtLastName.Name = "txtLastName" Me.txtLastName.Size = New System.Drawing.Size(136, 20) Me.txtLastName.TabIndex = 23 Me.txtLastName.Text = "Last Name" " "txtFirstName " Me.txtFirstName.Location = New System.Drawing.Point(72, 80) Me.txtFirstName.Name = "txtFirstName" Me.txtFirstName.Size = New System.Drawing.Size(136, 20) Me.txtFirstName.TabIndex = 22 Me.txtFirstName.Text = "First Name" " "Label2 " Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(8, 104) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(54, 13) Me.Label2.TabIndex = 21 Me.Label2.Text = "Last Name" " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(8, 80) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(53, 13) Me.Label1.TabIndex = 20 Me.Label1.Text = "First Name" " "lblDropTarget " Me.lblDropTarget.AllowDrop = True Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDropTarget.Location = New System.Drawing.Point(224, 8) Me.lblDropTarget.Name = "lblDropTarget" Me.lblDropTarget.Size = New System.Drawing.Size(200, 64) Me.lblDropTarget.TabIndex = 19 Me.lblDropTarget.Text = "Drop Target" " "lblDragSource " Me.lblDragSource.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDragSource.Location = New System.Drawing.Point(8, 8) Me.lblDragSource.Name = "lblDragSource" Me.lblDragSource.Size = New System.Drawing.Size(200, 64) Me.lblDragSource.TabIndex = 18 Me.lblDragSource.Text = "Drag Source" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(433, 177) Me.Controls.Add(Me.lblLastName) Me.Controls.Add(Me.lblFirstName) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.btnShowSerialization) Me.Controls.Add(Me.txtLastName) Me.Controls.Add(Me.txtFirstName) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.lblDropTarget) Me.Controls.Add(Me.lblDragSource) Me.Name = "Form1" Me.Text = "DragStudent2" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents lblLastName As System.Windows.Forms.Label Friend WithEvents lblFirstName As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents btnShowSerialization As System.Windows.Forms.Button Friend WithEvents txtLastName As System.Windows.Forms.TextBox Friend WithEvents txtFirstName As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents lblDropTarget As System.Windows.Forms.Label Friend WithEvents lblDragSource As System.Windows.Forms.Label
End Class
</source>
Drag and drop TreeView
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO 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 treeOne As System.Windows.Forms.TreeView Friend WithEvents Splitter1 As System.Windows.Forms.Splitter Friend WithEvents treeTwo As System.Windows.Forms.TreeView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.treeOne = New System.Windows.Forms.TreeView() Me.Splitter1 = New System.Windows.Forms.Splitter() Me.treeTwo = New System.Windows.Forms.TreeView() Me.SuspendLayout() " "treeOne " Me.treeOne.Dock = System.Windows.Forms.DockStyle.Left Me.treeOne.HideSelection = False Me.treeOne.ImageIndex = -1 Me.treeOne.Location = New System.Drawing.Point(5, 5) Me.treeOne.Name = "treeOne" Me.treeOne.SelectedImageIndex = -1 Me.treeOne.Size = New System.Drawing.Size(236, 351) Me.treeOne.TabIndex = 0 " "Splitter1 " Me.Splitter1.Location = New System.Drawing.Point(241, 5) Me.Splitter1.Name = "Splitter1" Me.Splitter1.Size = New System.Drawing.Size(3, 351) Me.Splitter1.TabIndex = 2 Me.Splitter1.TabStop = False " "treeTwo " Me.treeTwo.Dock = System.Windows.Forms.DockStyle.Fill Me.treeTwo.HideSelection = False Me.treeTwo.ImageIndex = -1 Me.treeTwo.Location = New System.Drawing.Point(244, 5) Me.treeTwo.Name = "treeTwo" Me.treeTwo.SelectedImageIndex = -1 Me.treeTwo.Size = New System.Drawing.Size(271, 351) Me.treeTwo.TabIndex = 3 " "TreeViewExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(520, 366) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeTwo, Me.Splitter1, Me.treeOne}) Me.DockPadding.Bottom = 10 Me.DockPadding.Left = 5 Me.DockPadding.Right = 5 Me.DockPadding.Top = 5 Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "TreeViewExample" Me.Text = "TreeView Example" Me.ResumeLayout(False) End Sub
- End Region
Private Sub TreeViewExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim node As TreeNode node = treeOne.Nodes.Add("Node 1") node.Nodes.Add("Leap 1") node.Nodes.Add("Leap 2") node.Expand() node = treeTwo.Nodes.Add("Node 2") node.Nodes.Add("Leap 3") node.Nodes.Add("Leap 4") node.Expand() treeTwo.AllowDrop = True treeOne.AllowDrop = True End Sub Private Sub treeOne_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles treeOne.MouseDown, treeTwo.MouseDown Dim tree As TreeView = CType(sender, TreeView) Dim node As TreeNode node = tree.GetNodeAt(e.X, e.Y) tree.SelectedNode = node If Not node Is Nothing Then tree.DoDragDrop(node.Clone(), DragDropEffects.Copy) End If End Sub Private Sub treeTwo_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles treeOne.DragOver, treeTwo.DragOver Dim tree As TreeView = CType(sender, TreeView) e.Effect = DragDropEffects.None If Not e.Data.GetData(GetType(TreeNode)) Is Nothing Then Dim pt As New Point(e.X, e.Y) pt = tree.PointToClient(pt) Dim node As TreeNode = tree.GetNodeAt(pt) If Not node Is Nothing Then e.Effect = DragDropEffects.Copy tree.SelectedNode = node End If End If End Sub Private Sub treeTwo_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles treeOne.DragDrop, treeTwo.DragDrop Dim tree As TreeView = CType(sender, TreeView) Dim pt As New Point(e.X, e.Y) pt = tree.PointToClient(pt) Dim node As TreeNode = tree.GetNodeAt(pt) node.Nodes.Add(e.Data.GetData(GetType(TreeNode))) node.Expand() End Sub
End Class
</source>
ListBox drag and drop to a TextBox
<source lang="vbnet"> 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
</source>
Make sure the drop data is Text
<source lang="vbnet"> Imports System Imports System.Runtime.InteropServices Imports System.Drawing Imports System.ruponentModel Imports System.Windows.Forms Imports System.IO Imports System.Xml.Serialization 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
" Start a drag. Private Sub lblDragSource_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles lblDragSource.MouseDown lblDragSource.DoDragDrop("Data!", DragDropEffects.Copy) End Sub " Make sure the drag is coming from lblDragSource. Private Sub lblDropTarget_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragEnter " See if the drag data includes text. If e.Data.GetDataPresent("Text") Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub " Display the dropped data. Private Sub lblDropTarget_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles lblDropTarget.DragDrop MessageBox.Show(e.Data.GetData("Text").ToString) 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.lblDragSource = New System.Windows.Forms.Label Me.lblDropTarget = New System.Windows.Forms.Label Me.SuspendLayout() " "lblDragSource " Me.lblDragSource.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDragSource.Location = New System.Drawing.Point(16, 16) Me.lblDragSource.Name = "lblDragSource" Me.lblDragSource.Size = New System.Drawing.Size(96, 64) Me.lblDragSource.TabIndex = 0 Me.lblDragSource.Text = "Drag Source" Me.lblDragSource.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lblDropTarget " Me.lblDropTarget.AllowDrop = True Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDropTarget.Location = New System.Drawing.Point(168, 16) Me.lblDropTarget.Name = "lblDropTarget" Me.lblDropTarget.Size = New System.Drawing.Size(96, 64) Me.lblDropTarget.TabIndex = 1 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(279, 95) Me.Controls.Add(Me.lblDropTarget) Me.Controls.Add(Me.lblDragSource) Me.Name = "Form1" Me.Text = "LabelDrag" Me.ResumeLayout(False) End Sub Friend WithEvents lblDragSource As System.Windows.Forms.Label Friend WithEvents lblDropTarget As System.Windows.Forms.Label
End Class
</source>
Simple Drag and Drop Event Illustration
<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms Imports System.Math Imports System.Xml.Serialization Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class
Public Class Form1
Private m_Dragging As Boolean Private Sub lblDragSource_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblDragSource.MouseDown m_Dragging = True lblDragSource.DoDragDrop("Text from source", DragDropEffects.Copy) m_Dragging = False End Sub Private Sub lblDropTarget_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragEnter If m_Dragging Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub lblDropTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lblDropTarget.DragDrop MessageBox.Show(e.Data.GetData(DataFormats.Text).ToString) 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.lblDropTarget = New System.Windows.Forms.Label Me.lblDragSource = New System.Windows.Forms.Label Me.SuspendLayout() " "lblDropTarget " Me.lblDropTarget.AllowDrop = True Me.lblDropTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDropTarget.Location = New System.Drawing.Point(160, 8) Me.lblDropTarget.Name = "lblDropTarget" Me.lblDropTarget.Size = New System.Drawing.Size(120, 96) Me.lblDropTarget.TabIndex = 3 Me.lblDropTarget.Text = "Drop Target" Me.lblDropTarget.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "lblDragSource " Me.lblDragSource.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDragSource.Location = New System.Drawing.Point(8, 8) Me.lblDragSource.Name = "lblDragSource" Me.lblDragSource.Size = New System.Drawing.Size(120, 96) Me.lblDragSource.TabIndex = 2 Me.lblDragSource.Text = "Drag Source" Me.lblDragSource.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(288, 114) Me.Controls.Add(Me.lblDropTarget) Me.Controls.Add(Me.lblDragSource) Me.Name = "Form1" Me.Text = "DragWithinApp" Me.ResumeLayout(False) End Sub Friend WithEvents lblDropTarget As System.Windows.Forms.Label Friend WithEvents lblDragSource As System.Windows.Forms.Label
End Class
</source>
Tree drag and drop to a TextBox
<source lang="vbnet"> 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
</source>