General discussion

Locked

Two List Box (to Add & remove items )

By thakker123 ·
Hello
i have to findout what is wrong in this syntex
i have 2 list box
user can select multiple items and click on Cmdadd then that item is to add in list2 & also get remove from list1

below is the syntex

Private Sub CmdRem_Click()
If List2.SelCount = 1 Then
List1.AddItem List2.List(List2.ListIndex)
List2.RemoveItem List2.ListIndex
ElseIf List2.SelCount > 1 Then
Dim b
b = (List2.ListCount - 1)
For a = 0 To b
If List2.Selected(a) = True Then
List1.AddItem List2.List(List2.ListIndex)
List2.RemoveItem List2.ListIndex
End If
Next
Else
MsgBox "Please select the item to remove from savein box"
End If
End Sub


Thanks
athakker

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Two List Box (to Add & remove items )

by MadestroITSolutions In reply to Two List Box (to Add & r ...

My dear friend, the error is quite simple, and lies on the multiple selection condition:
you are running a for loop that is going to go from a = to b (b being the list count) correct?
Now, if you remove an item during the loop, you think b is still an accurate list count???
Of course not, the listcount is now equal to (b-1), so what you have to do is substract 1 from b every time you delete an item.
NOW HERE IS THE INTERESTING PART:

Even when you substract 1 from b, the iterator still runs up to the original count. I have no idea why, either you cannot change the end value for the iterator, or YOU JUST FOUND A BUG IN VB!, he he..... I reproduced the error you were having, but even after fixing it to run properly, I found out that the for loop would still allow the operations to be executed even when the condition met!.... I could not believe what I was seeing.... the iterator is still going into the instructions although the
comparison returns positive.
Anyways, to make a long story short, don't use the stupid for loop. Use a while wend instead. Here is the code (tested, works 100%):

b = (List2.ListCount - 1)
a = 0
While a <= b
If List2.Selected(a) = True Then
List1.AddItem List2.List(a)
List2.RemoveItem a
List2.Refresh
Debug.Print "Removed item"
b = b - 1
a = -1
End If
a = a + 1
Wend

Put this code instead of the code in the [elseif list2.selcount - 1] then section.

Good luck!

Collapse -

Two List Box (to Add & remove items )

by MadestroITSolutions In reply to Two List Box (to Add & r ...

I forgot to remove the debug.print line, just take it out, unless you want to see how it runs.
Questions or comments? feel free to drop a line at juanr@akaconsulting.com
cya!

Collapse -

Two List Box (to Add & remove items )

by thakker123 In reply to Two List Box (to Add & r ...

Thanks for ur reply,
I have tried Private Sub CmdAdd_Click()
If List1.SelCount = 1 Then
List2.AddItem List1.List(List1.ListIndex)
List1.RemoveItem List1.ListIndex
ElseIf List1.SelCount > 1 Then
Dim a, b
b = (List1.ListCount - 1)
MsgBox " B(ListCount) = " & b
For a = b To 0 Step -1
If List1.Selected(a) = True Then
List1.ListIndex = a
List2.AddItem List1.List(List1.ListIndex)
List1.RemoveItem List1.ListIndex End If
Next
ElseIf List1.SelCount = 0 Then
MsgBox "Please Select the section"
List1.SetFocus
End If
End Sub

Private Sub CmdClr_Click()
Dim b, a
b = (List2.ListCount - 1)
For a = b To 0 Step -1
List2.ListIndex =a
List1.AddItem List2.List(a)
List2.RemoveItem List2.ListIndex
Next
End Sub

Private Sub CmdRem_Click()
If List2.SelCount = 1 Then
List1.AddItem List2.List(List2.ListIndex)
List2.RemoveItem List2.ListIndex
ElseIf List2.SelCount > 1 Then
Dim b, a
b = (List2.ListCount - 1)
For a = b To 0 Step -1
If List2.Selected(a) = True Then
List2.ListIndex = a
List1.AddItem List2.List(List2.ListIndex)
List2.RemoveItem List2.ListIndex
EndIf
Next
Else
MsgBox "Please select the item to remove from savein box"
End If
End Subwith the following code & this works successfully

Collapse -

Two List Box (to Add & remove items )

by thakker123 In reply to Two List Box (to Add & r ...

This question was closed by the author

Back to Web Development Forum
4 total posts (Page 1 of 1)  

Related Discussions

Software Forums