Items not removing from listbox

kdeutsch
kdeutsch used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
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


Commented:

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

Senior Software Engineer
Commented:
Simply use the following to remove the selected item:
lbUic.Items.RemoveAt(lbUic.SelectedIndex)

Open in new window

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

See On Planet-source-code.com
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
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

Author

Commented:
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.

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial