ListBox.DragDrop : ListBox « System.Windows.Forms « VB.Net by API

Home
VB.Net by API
1.Microsoft.VisualBasic
2.Microsoft.Win32
3.System
4.System.Collections
5.System.Collections.Generic
6.System.Collections.Specialized
7.System.ComponentModel
8.System.Configuration.ConfigurationSettings
9.System.Data
10.System.Data.Odbc
11.System.Data.OleDb
12.System.Data.OracleClient
13.System.Data.SqlClient
14.System.Diagnostics
15.System.Drawing
16.System.Drawing.Drawing2D
17.System.Drawing.Imaging
18.System.Drawing.Printing
19.System.Drawing.Text
20.System.Globalization
21.System.IO
22.System.IO.IsolatedStorage
23.System.IO.Ports
24.System.Media
25.System.Messaging
26.System.Net
27.System.Net.Sockets
28.System.Reflection
29.System.Runtime.InteropServices
30.System.Runtime.Remoting.Channels
31.System.Runtime.Serialization
32.System.Runtime.Serialization.Formatters.Binary
33.System.Runtime.Serialization.Formatters.Soap
34.System.Security.Cryptography
35.System.Security.Cryptography.X509Certificates
36.System.Security.Permissions
37.System.Security.Principal
38.System.ServiceProcess
39.System.Text
40.System.Text.RegularExpressions
41.System.Threading
42.System.Web
43.System.Web.Mail
44.System.Windows.Forms
45.System.Xml
46.System.Xml.Schema
47.System.Xml.Serialization
48.System.Xml.Xsl
VB.Net
VB.Net Tutorial
VB.Net by API » System.Windows.Forms » ListBox 
ListBox.DragDrop
  


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)
        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

    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.ListBox.DataSource
2.ListBox.DisplayMember
3.ListBox.DoDragDrop
4.ListBox.DoubleClick
5.ListBox.DragEnter
6.ListBox.DragLeave
7.ListBox.DragOver
8.ListBox.DrawMode
9.ListBox.IndexFromPoint
10.ListBox.ItemHeight
11.ListBox.Items.Add
12.ListBox.Items.Clear()
13.ListBox.Items.Remove
14.ListBox.Items.RemoveAt
15.ListBox.MeasureItem
16.ListBox.MouseDown
17.ListBox.PointToClient
18.ListBox.SelectedIndexChanged
19.ListBox.SelectedIndices
20.ListBox.SelectionMode
21.ListBox.SetSelected
22.ListBox.TopIndex
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.