[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

add/remove button between listboxes

Hi, i'm trying to move the item from listbox1 to listbox2 (i'm populating the item from database and put into the listbox1 anyway) by using add / remove buttons. When I click add button, the selected item will move to listbox 2 , as soon as I click remove, the item will go back to listbox1 from listbox2. The item must be gone in listbox1 as soon as i click add button and appear in listbox2 and vice versa.

this is my code :

Private Sub cmdadd_Click()
If lstSelect.ListIndex <> -1 Then
lstMove.AddItem lstSelect.List(lstSelect.ListIndex)
lstSelect.RemoveItem (lstSelect.ListIndex)
End If
End Sub

Private Sub cmdrenove_Click()
If lstMove.ListCount > 0 Then
    If lstMove.ListIndex <> -1 Then
        lstMove.RemoveItem lstMove.ListIndex
        lstSelect.AddItem lstMove.ListIndex
    End If
End If
End Sub

can anyone tell me what's wrong with this code ?? I think the add button works well, but the remove button doesn't . Can anyone help me ?
0
tabing16
Asked:
tabing16
1 Solution
 
corvanderlindenCommented:
Private Sub cmdrenove_Click()
If lstMove.ListCount > 0 Then
   If lstMove.ListIndex <> -1 Then
'!!!!!!!!!!!!
       lstSelect.AddItem lstMove.ListIndex
       lstMove.RemoveItem lstMove.ListIndex
'!!!!!!!!!!!!
   End If
End If
End Sub

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
hhmmmm, minor correction:

Private Sub cmdrenove_Click()
  If lstMove.Listindex <>-1 Then
'!!!!!!!!!!!!
      lstSelect.AddItem lstMove.List(lstMove.ListIndex)
      lstMove.RemoveItem lstMove.ListIndex
'!!!!!!!!!!!!
  End If
End Sub

Cheers
0
 
bob_onlineCommented:
The others code looks right, but they didn't explain it.  You have to add the item to one list box BEFORE removing it from the other -- or it's lost.
0
 
tabing16Author Commented:
thx for the lovely code, you deserve the points
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
well seen & nice explanation, bob_online!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now