Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Avoid duplicate items in listbox 2

Status
Not open for further replies.

clanm

Programmer
Dec 26, 2005
237
US
I have two list boxes. The one of the left has available numbers, and the right is what the user will build for "selected" numbers.
I can pull them over to the right "selected" side, and allow users to delete them from the right if they make a mistake.

What I want to do is NOT allow users to add duplicate values on the right side.

Any suggestions?

Thanks!
 
Code:
        Dim li As New ListItem
        For Each li In ListBox1.Items
            If li.Selected = True Then
                If Not ListBox2.Items.Contains(li) Then
                    ListBox2.Items.Add(li)
                End If
            End If
        Next

Jim
 
Hello!

I used this code, and it works to not allow duplicates if I stay on the selected item in the list box and try to click it multiple times:

Dim li As New ListItem
For Each li In LB1.Items
If li.Selected = True Then
If Not ListBox2.Items.Contains(li) Then
ListBox2.Items.Add(li)
End If
End If
Next

If I go to another list item, say the one directly below, the go back to the initial item, I can in fact add duplicates. Any ideas? I'll keep searching too.

Thanks!
 
It is working perfectly for me. It should not allow it to add if it is already in the list. I have tested it many times.

Let me know what you find.
 
jbenson001,

It would help if I put the right code in the correct listbox.

My fault...you're 100% right...it works fine. I had to set the mode to multiple select.

thanks again!
 
jbenson001,

Any ideas on how to get the items ASC orded when they're added? I'll keep looking.

Thanks again for the help!
 
If you use an Array to store the item's values, then you can use the Sort method of the Array and then bind it to the ListBox.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm,

I was just looking into a SortedList.....

I'll try that too!

thanks!
 
ca8msm,

If I click on a second item to add it to my listbox, ListBox2, I get an error of:
At least one object must implement IComparable

Here's the code:

'Sort items ASC
Dim item As String
Dim items As New ArrayList(ListBox2.Items)
Dim x As Integer
For x = 0 To items.Count - 1
'step through the arraylist, one at a time & create string
item += items(x).ToString & ","
items.Sort()
Next
 
ca8msm,

I'm trying this code but get:
At least one object must implement IComparable.

Dim items As New ArrayList(ListBox2.Items)
items.Sort()
ListBox2.DataSource = items
 
glad you got it working ..and good example.. will have to keep that one on file :)

JIm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top