Option Explicit On 
Option Strict On

Public Class frmMove
  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
  Friend WithEvents lstBox1 As System.Windows.Forms.ListBox
  Friend WithEvents lstBox2 As System.Windows.Forms.ListBox
  Friend WithEvents btnMove As System.Windows.Forms.Button

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.Container

  '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.lstBox1 = New System.Windows.Forms.ListBox()
    Me.btnMove = New System.Windows.Forms.Button()
    Me.lstBox2 = New System.Windows.Forms.ListBox()
    Me.SuspendLayout()
    '
    'lstBox1
    '
    Me.lstBox1.Location = New System.Drawing.Point(16, 24)
    Me.lstBox1.Name = "lstBox1"
    Me.lstBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple
    Me.lstBox1.Size = New System.Drawing.Size(168, 147)
    Me.lstBox1.Sorted = True
    Me.lstBox1.TabIndex = 0
    '
    'btnMove
    '
    Me.btnMove.Location = New System.Drawing.Point(24, 192)
    Me.btnMove.Name = "btnMove"
    Me.btnMove.Size = New System.Drawing.Size(152, 24)
    Me.btnMove.TabIndex = 1
    Me.btnMove.Text = "&Move Selected Items"
    '
    'lstBox2
    '
    Me.lstBox2.Location = New System.Drawing.Point(208, 24)
    Me.lstBox2.Name = "lstBox2"
    Me.lstBox2.Size = New System.Drawing.Size(168, 147)
    Me.lstBox2.TabIndex = 0
    '
    'frmMove
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(408, 235)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnMove, Me.lstBox2, Me.lstBox1})
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    Me.Name = "frmMove"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "List Box Problem"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub frmMove_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim State(50) As String
    Dim I As Integer
    'Load state arrays (you can use another list)
    State(1) = "Alabama" : State(2) = "Alaska"
    State(3) = "Arizona" : State(4) = "Arkansas"
    State(5) = "California" : State(6) = "Colorado"
    State(7) = "Connecticut" : State(8) = "Delaware"
    State(9) = "Florida" : State(10) = "Georgia"
    State(11) = "Hawaii" : State(12) = "Idaho"
    State(13) = "Illinois" : State(14) = "Indiana"
    State(15) = "Iowa" : State(16) = "Kansas"
    State(17) = "Kentucky" : State(18) = "Louisiana"
    State(19) = "Maine" : State(20) = "Maryland"
    State(21) = "Massachusetts" : State(22) = "Michigan"
    State(23) = "Minnesota" : State(24) = "Mississippi"
    State(25) = "Missouri" : State(26) = "Montana"
    State(27) = "Nebraska" : State(28) = "Nevada"
    State(29) = "New Hampshire" : State(30) = "New Jersey"
    State(31) = "New Mexico" : State(32) = "New York"
    State(33) = "North Carolina" : State(34) = "North Dakota"
    State(35) = "Ohio" : State(36) = "Oklahoma"
    State(37) = "Oregon" : State(38) = "Pennsylvania"
    State(39) = "Rhode Island" : State(40) = "South Carolina"
    State(41) = "South Dakota" : State(42) = "Tennessee"
    State(43) = "Texas" : State(44) = "Utah"
    State(45) = "Vermont" : State(46) = "Virginia"
    State(47) = "Washington" : State(48) = "West Virginia"
    State(49) = "Wisconsin" : State(50) = "Wyoming"
    'Clear list boxes
    lstBox1.Items.Clear()
    lstBox2.Items.Clear()
    'Populate list box 1
    For I = 1 To 50
      lstBox1.Items.Add(State(I))
    Next I
  End Sub

  Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMove.Click
    'Move selected items to list box 2
    'note count changes each time one is moved
    'always moving and removing top (0th) element since indices change
    Do Until lstBox1.SelectedItems.Count = 0
      lstBox2.Items.Add(lstBox1.Items(lstBox1.SelectedIndices(0)))
      lstBox1.Items.RemoveAt(lstBox1.SelectedIndices(0))
    Loop
  End Sub


  Private Sub lstBox2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstBox2.DoubleClick
    'Move selected item back to list box 1
    lstBox1.Items.Add(lstBox2.Items(lstBox2.SelectedIndex))
    lstBox2.Items.RemoveAt(lstBox2.SelectedIndex)
  End Sub
End Class
