Jump to content

So I am taking computer science and we need to create a 2 list boxes that will transfer an item from one to the other and then be able to delete from the second list box. I have done that, but want to make sure that someone cannot add an item twice, which they can at the minute. Any way?

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
https://linustechtips.com/topic/662253-visual-basic-list-box/
Share on other sites

Link to post
Share on other sites

Just loop through items on the list and check if they're equal, if you find same item on the list then interrupt execution of adding function, if item hasn't been found add it.

 

For Each item As String In ListBox1.Items
  If item = TextBox1.Text Then
    MessageBox.Show("Item " + TextBox1.Text + " is already on the list.")
    Return
  End If
Next

ListBox1.Items.Add(TextBox1.Text)

You can implement it when you add new item on first listbox, and also when you copy items from one to the another, or you can check in both listboxes in first place then you don't need to check it again when copying.

Link to comment
https://linustechtips.com/topic/662253-visual-basic-list-box/#findComment-8558997
Share on other sites

Link to post
Share on other sites

1 hour ago, Mr_KoKa said:

Just loop through items on the list and check if they're equal, if you find same item on the list then interrupt execution of adding function, if item hasn't been found add it.

 


For Each item As String In ListBox1.Items
  If item = TextBox1.Text Then
    MessageBox.Show("Item " + TextBox1.Text + " is already on the list.")
    Return
  End If
Next

ListBox1.Items.Add(TextBox1.Text)

You can implement it when you add new item on first listbox, and also when you copy items from one to the another, or you can check in both listboxes in first place then you don't need to check it again when copying.

Cheers, I think I understand now, but I wouldn't be able to do the last bit that you suggested as it the first list is populated before runtime and the user selects items from the list and adds them to the second list box, but keeping them present in the first list box. The trouble is you can copy the same item twice into the second list box

There are 10 types of people in the world. Those that understand binary and those that don't.

Link to comment
https://linustechtips.com/topic/662253-visual-basic-list-box/#findComment-8559380
Share on other sites

Link to post
Share on other sites

Ok, I thought you populate first listbox on runtime and then you move all to the second one.

When you move one item from first listbox you probably use code that looks like this:

 

If ListBox1.SelectedIndex != -1 Then
	ListBox2.Items.Add(ListBox1.Items(ListBox1.SelectedIndex))
End If

You can just assign item you want to move to variable and compare existing items against it.

If ListBox1.SelectedIndex != -1 Then
        Dim newItem As String = ListBox1.Items(ListBox1.SelectedIndex)

        For Each item As String In ListBox2.Items
            If item = newItem Then
                MessageBox.Show("Item " + newItem + " is already on the list.")
                Return
            End If
        Next

        ListBox2.Items.Add(newItem)
End If

 

Link to comment
https://linustechtips.com/topic/662253-visual-basic-list-box/#findComment-8560419
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×