[RESOLVED] Something goes wrong when I try to retrieve a value from a listbox control
I am trying to retrieve a value from a listbox, but for some reason, what gets retrieved is the number of items that has been added to the listbox. Here is my code:
Code:
For i = 0 To Me.ExerciseListBox.Items.Count - 1
exerciseArray.Add(Me.ExerciseListBox.Items.Item(i))
numberOfSetsArray.Add(Me.NumberOfSetsListBox.Items(i))
numberOfRepsArray.Add(Me.NumberOfRepsListBox.Items(i))
restArray.Add(Me.RestListBox.Items(i))
dimensionArray.Add(Me.DimensionListBox.Items(i))
Next
Here is the source code for the entire sub procedure.
Code:
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Dim s As New SessionData()
Dim i As Integer = 0
Dim exerciseArray As New ArrayList
Dim numberOfSetsArray As New ArrayList
Dim numberOfRepsArray As New ArrayList
Dim restArray As New ArrayList
Dim dimensionArray As New ArrayList
'clear the arraylist
exerciseArray.Clear()
numberOfSetsArray.Clear()
numberOfRepsArray.Clear()
restArray.Clear()
dimensionArray.Clear()
For i = 0 To Me.ExerciseListBox.Items.Count - 1
exerciseArray.Add(Me.ExerciseListBox.Items.Item(i))
numberOfSetsArray.Add(Me.NumberOfSetsListBox.Items(i))
numberOfRepsArray.Add(Me.NumberOfRepsListBox.Items(i))
restArray.Add(Me.RestListBox.Items(i))
dimensionArray.Add(Me.DimensionListBox.Items(i))
Next
sessiondata = New SessionData(Me.SelectAClientComboBox.Text, Me.DateTimePicker1.Text, Me.StageComboBox.Text, _
exerciseArray, numberOfSetsArray, numberOfRepsArray, restArray, dimensionArray)
Me.sessionDataArrayList.Add(sessiondata)
MsgBox("Data has been saved.", MsgBoxStyle.Information, "Data saved")
Me.sessiondata.XMLWrite()
End Sub
It appears that for some reason Me.ExerciseListBox.Items.Count is being retrieved instead of the item that I stored in there.
If you want to know how I added the information:
Code:
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
If Me.DimensionsComboBox.Text = "min" Or Me.DimensionsComboBox.Text = "sec" Then
Else
MsgBox("Please select either second or minute for the rest period.", MsgBoxStyle.Information, "Select sec or min")
Exit Sub
End If
Me.ExerciseListBox.Items.Add(Me.ExerciseTextBox.Text)
Me.NumberOfSetsListBox.Items.Add(Me.NumberOfSetsTextBox.Text)
Me.NumberOfRepsListBox.Items.Add(Me.NumberOfRepsTextBox.Text)
Me.RestListBox.Items.Add(Me.RestTextBox.Text)
Me.DimensionListBox.Items.Add(Me.DimensionsComboBox.Text)
End Sub
Re: Something goes wrong when I try to retrieve a value from a listbox control
I figured out what was causing my errors. I forgot all about the rules of ArrayList. An arraylist can hold anything, but when you retrieve the string information from a method that searches for your information by uses an integer, you may get weird results just like I experienced. So here is how I fixed it.
Code:
exerciseArray.Add(New String(Me.ExerciseListBox.Items.Item(i)))
Re: Something goes wrong when I try to retrieve a value from a listbox control
Or even
Code:
exerciseArray.Add(Me.ExerciseListBox.Items(i).ToString)
Please marked this thread Resolved if your question has been answered.
Re: Something goes wrong when I try to retrieve a value from a listbox control
Code:
exerciseArray.Add(Me.ExerciseListBox.Items(i).ToString)
This didn't work for me, because that was one of the first things I tried. It still was returning a count of all the elements in the exerciselistbox. I had to actually create an arraylist of string objects by using the new string keyword.
Re: [RESOLVED] Something goes wrong when I try to retrieve a value from a listbox control
Would a List work then?
vb Code:
Dim exerciseList As New List(of String)
'
exerciseList.Add(Me.Exerciselistbox.Items(i).toString)