Link to home
Start Free TrialLog in
Avatar of kdeutsch
kdeutschFlag for United States of America

asked on

Items not removing from listbox

I have a list box that is populated with 2 different items depending on which options they choose.  The first is just a alpha code called a uic  and its value and text shown are the same.
the next is a name and for this I bind the name and ssn of a person.  When I try to remove a UIC it works fine and removes from the listbox, but when I try to remove a name it does not work and the name stays.  what can I do so that it removes, names or UIcs.

'Code for the Uic Section of the program
    Protected Sub txtUic_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUic.TextChanged
        lbUic.Items.Add(HFID.Value)
        txtUic.Text = String.Empty
    End Sub

    Protected Sub lbUic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbUic.SelectedIndexChanged
        Dim Selected As String = lbUic.SelectedItem.ToString
        Dim Value As String = lbUic.SelectedValue
       
        lbUic.Items.Remove(Selected)
        lbUic.Items.Remove(Value)
    End Sub

    'All the code for the Fill Personnel section of program
    Protected Sub txtPersonnel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPersonnel.TextChanged

        lbUic.Items.Add(New ListItem(txtPersonnel.Text, HFID.Value))

        txtPersonnel.Text = String.Empty
    End Sub

Open in new window

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

is your listbox capable of select multiple items?
listBox1.SelectionMode = SelectionMode.MultiExtended

You should remove with this code:
While lbUic.SelectedItems.Count > 0
      lbUic.Items.Remove(myListBox.SelectedItems(0))
End While



For Each item As ListItem In ListBox1.Items
	If lstBox1.Items.FindByText(item.Text) IsNot Nothing Then
	lstBox1.Items.Remove(lstBox1.Items.FindByText(item.Text))
	End If
Next

ListBox1.ClearSelection()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yogesh28577
yogesh28577

See On Planet-source-code.com
Just to complement my previous post, replace your code with:
Protected Sub lbUic_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbUic.SelectedIndexChanged
        While lbUic.SelectedItems.Count > 0
           lbUic.Items.Remove(myListBox.SelectedItems(0))
        End While
End Sub

Avatar of kdeutsch

ASKER

All,

Ok got around to trying all these this morning,
mas_oz2003:
I could not get count after selecteditem as there is no selecteditems and no count for either one of them.

disrupt:
did not work

Medo3337:
Worked as advertised.
thanks