Results 1 to 5 of 5

Thread: [RESOLVED] Something goes wrong when I try to retrieve a value from a listbox control

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    Resolved [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

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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)))
    Last edited by kevin_10987; Jun 6th, 2007 at 01:57 PM.

  3. #3
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.
    Prefix has no suffix, but suffix has a prefix.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    35

    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.

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] Something goes wrong when I try to retrieve a value from a listbox control

    Would a List work then?

    vb Code:
    1. Dim exerciseList As New List(of String)
    2. '
    3. exerciseList.Add(Me.Exerciselistbox.Items(i).toString)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width