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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectIndex on Listbox isn't working

Status
Not open for further replies.

Halliarse

IS-IT--Management
Jan 8, 2007
213
GB
Good Afternoon

I have what appears to me a basic problem but I cannot resolve it and I'm pully my hair out (Something I can ill afford to do!!

Within my Form_Load I am populating my Listbox dynmically with data from a table (Code below, very basic!) The list populates exactly as I would expect!

cmd.CommandText = "SELECT * from Item"

Try
Dim reader As SqlDataReader = cmd.ExecuteReader

If reader.HasRows Then
While reader.Read
ItemNumber = reader.Item("No_").ToString
ItemNumber = LSet(ItemNumber, 18)
Description = reader.Item("Description").ToString
ListBox1.Items.Add(ItemNumber & "|" & Description)
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

However, I have code that displays the change in selected index which appears to work correctly for the first 2 attempts, but on the 3rd and subsequent, displays the index value from the 2nd selection (I thnk that makes sense!)

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(ListBox1.SelectedIndex)
End Sub

Any advise to stop the impending baldness would be gratefully received!

Many thanks

Steve
 
Please ignore this thread as I have now resolved it myself!
 
Hi Haliarse, What was the cause of your problem? Lots of us would like to learn in case we come across this too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top