Drag and drop between two ListBox : ListBox « GUI « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » GUI » ListBoxScreenshots 
Drag and drop between two ListBox
Drag and drop between two ListBox
 
Imports System.Drawing.Drawing2D
Imports System
Imports System.Drawing.Text
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Math

Public Class MainClass

   Shared Sub Main()
       Dim form1 As Form = New Form1()
       Application.Run(form1)
   End Sub 

End Class

Public Class Form1
    Private m_DragSource As ListBox = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, _
     ByVal e As System.EventArgsHandles MyBase.Load
        lstUnselected.AllowDrop = True
        lstSelected.AllowDrop = True

        ' Add event handlers.
        AddHandler lstUnselected.MouseDown, AddressOf List_MouseDown
        AddHandler lstUnselected.DragOver, AddressOf List_DragOver
        AddHandler lstUnselected.DragDrop, AddressOf List_DragDrop
        AddHandler lstUnselected.DragLeave, AddressOf List_DragLeave

        AddHandler lstSelected.MouseDown, AddressOf List_MouseDown
        AddHandler lstSelected.DragOver, AddressOf List_DragOver
        AddHandler lstSelected.DragDrop, AddressOf List_DragDrop
        AddHandler lstSelected.DragLeave, AddressOf List_DragLeave
    End Sub

    Private Sub List_MouseDown(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.MouseEventArgs)
        ' Select the item at this point.
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        this_list.SelectedIndex = this_list.IndexFromPoint(e.X, e.Y)
        If this_list.SelectedIndex < Then Exit Sub

        m_DragSource = this_list

        this_list.DoDragDrop_
            this_list.SelectedItem.ToString, _
            DragDropEffects.Move)

        m_DragSource = Nothing
    End Sub

    ' Highlight the item under the mouse.
    Private Sub List_DragOver(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DragEventArgs)
        If m_DragSource Is Nothing Then Exit Sub

        e.Effect = DragDropEffects.Move
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        Dim pt As Point = _
            this_list.PointToClient(New Point(e.X, e.Y))
        Dim drop_index As Integer = _
            this_list.IndexFromPoint(pt.X, pt.Y)
        this_list.SelectedIndex = drop_index
    End Sub

    Private Sub List_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        this_list.SelectedIndex = -1
    End Sub

    ' Accept the drop.
    Private Sub List_DragDrop(ByVal sender As Object, _
     ByVal e As System.Windows.Forms.DragEventArgs)
        Dim this_list As ListBox = DirectCast(sender, ListBox)
        MoveItem(e.Data.GetData(DataFormats.Text).ToString, _
            m_DragSource, this_list, e.X, e.Y)
    End Sub

    ' Move the value txt from drag_source to drop_target.
    Private Sub MoveItem(ByVal txt As String, ByVal drag_source As ListBox, _
     ByVal drop_target As ListBox, ByVal X As Integer, ByVal Y As Integer)
        Dim drop_index As Integer = drop_target.SelectedIndex
        If drop_index < Then
            drop_index = drop_target.Items.Add(txt)
        Else
            drop_target.Items.Insert(drop_target.SelectedIndex, txt)
        End If

        drop_target.SelectedIndex = drop_index

        If drag_source Is drop_target Then
            Dim target_index As Integer = drag_source.FindStringExact(txt)
            If target_index = drop_index Then _
                target_index = drag_source.FindStringExact(txt, target_index)
            drag_source.Items.RemoveAt(target_index)
        Else
            drag_source.Items.Remove(txt)
        End If
    End Sub
End Class



<Global.Microsoft.VisualBasic.CompilerServices.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.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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.lstSelected = New System.Windows.Forms.ListBox
        Me.lstUnselected = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(1520)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(14416)
        Me.Label2.TabIndex = 11
        Me.Label2.Text = "Right"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(00)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(14416)
        Me.Label1.TabIndex = 10
        Me.Label1.Text = "Left"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'lstSelected
        '
        Me.lstSelected.FormattingEnabled = True
        Me.lstSelected.Items.AddRange(New Object() {"1""2""3""4""5""6""7""8"})
        Me.lstSelected.Location = New System.Drawing.Point(15216)
        Me.lstSelected.Name = "lstSelected"
        Me.lstSelected.Size = New System.Drawing.Size(144264)
        Me.lstSelected.TabIndex = 9
        '
        'lstUnselected
        '
        Me.lstUnselected.FormattingEnabled = True
        Me.lstUnselected.Items.AddRange(New Object() {"A""B""C""D""E""F""G""H"})
        Me.lstUnselected.Location = New System.Drawing.Point(016)
        Me.lstUnselected.Name = "lstUnselected"
        Me.lstUnselected.Size = New System.Drawing.Size(144264)
        Me.lstUnselected.TabIndex = 8
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(297281)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.lstSelected)
        Me.Controls.Add(Me.lstUnselected)
        Me.Name = "Form1"
        Me.Text = "DragBetweenListBoxes"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents lstSelected As System.Windows.Forms.ListBox
    Friend WithEvents lstUnselected As System.Windows.Forms.ListBox

End Class
 
           
         
  
Related examples in the same category
1.Toggling list box Selection Mode: select one or select more than oneToggling list box Selection Mode: select one or select more than one
2.Draw selected Item in ListBoxDraw selected Item in ListBox
3.Owner Draw ListBoxOwner Draw ListBox
4.Add Item to ListBoxAdd Item to ListBox
5.Add Item to a List Box by clicking a ButtonAdd Item to a List Box by clicking a Button
6.Get Selected Item from ListBoxGet Selected Item from ListBox
7.Add, remove and clear list box itemsAdd, remove and clear list box items
8.List Box and Spinner DemoList Box and Spinner Demo
9.Customize ListBox Font and Item HeightCustomize ListBox Font and Item Height
10.Set ListBox TopIndexSet ListBox TopIndex
11.Enum Binding
w__w___w__.__ja___v__a_2__s_.___c___o__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.