move all items from left to right

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

move all items from left to right

Post by sal21 »

I have 2 listbox named lstLeft and lstRight in a form.
I have inserted in form 2 button toleft and toright

When i click on button toright copy all items from lstLeft to lstRight and viceversa with the toright button..
How to?

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: move all items from left to right

Post by HansV »

Try:

Code: Select all

Private Sub toRight_Click()
    Dim i As Long
    ' Make sure that lstRight is empty
    Me.lstRight.Clear
    ' Loop through items of lstLeft
    For i = 0 To Me.lstLeft.ListCount - 1
        ' Add item to lstRight
        Me.lstRight.AddItem Me.lstLeft.List(i)
    Next i
    ' Clear lstLeft
    Me.lstLeft.Clear
End Sub
Similar for the other.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: move all items from left to right

Post by sal21 »

HansV wrote:Try:

Code: Select all

Private Sub toRight_Click()
    Dim i As Long
    ' Make sure that lstRight is empty
    Me.lstRight.Clear
    ' Loop through items of lstLeft
    For i = 0 To Me.lstLeft.ListCount - 1
        ' Add item to lstRight
        Me.lstRight.AddItem Me.lstLeft.List(i)
    Next i
    ' Clear lstLeft
    Me.lstLeft.Clear
End Sub
Similar for the other.
Wow! great code tks.