I have a listbox that contains numbers that vary from 0 to 99. My problem is when the number in the listbox is 0, listbox1.selectedindex will not return a value. So I cannot use removeat(x) which works for all other numbers. Can anyone give me an idea on how to do it ?

Post your code. When I used the following code it worked fine.

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.SelectedIndex
    End Sub

Thanks your code helped me getting the indexnumber. I use the indexnumber as an integer named si. I have the code ListBox1.Selected.Item.RemoveAt(si) under a button which will remove any number from the listbox except the number 0 even with the right indexnumber. I don't understand why

Found this to do the trick, I put the SelectedItem in a TextBox and use Me.ListBox1.Items.Remove(Me.TextBox1.Text)

I know it's been solved, but to illustrate further, if you add the following after line 2

If ListBox1.SelectedIndex = 0 Then
    ListBox1.RemoveAt(ListBox1.SelectedIndex)
End If

then clicking on the first element in the list (index=0) will remove it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.