ListBox drag and drop to a TextBox
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.ComponentModel.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
Related examples in the same category