Populating An Array With String Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zballer20011
    New Member
    • Jan 2017
    • 1

    Populating An Array With String Values

    How can I fill an array with string values from an InputBox and then display that in a ListBox?

    Code:
    Public Class frmQuiz
        Dim strQuestions(10) As String
    Private Sub btnQuestion2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuestion2.Click
            Dim strQuestion2 As String
            strQuestion2 = InputBox("What is the last name of the player who many consider to be the best player of all time?")
            strQuestions(1) = strQuestion2
        End Sub
     Private Sub btnGradeQuiz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGradeQuiz.Click
            MessageBox.Show("Your answers will be displayed. You may make changes to your response before you submit.")
            For Each Value As String In strQuestions
                lstAnswers.Items.Add(strQuestions)
    
    
            Next
    
    
    
    
    
    
        End Sub

    This does not work right and just displays "String[] Array"
    Last edited by Frinavale; Jan 12 '17, 02:05 PM. Reason: Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The ListBox.Items.A dd method adds a single item to the items and you are providing a whole array. It is displaying "String[] Array" because it is calling the ToString method on the array since it is not a single string.

    Provide the array to the Items property instead of using the Add method...or if you can't do that then loop through the array and add the items to the ListBox individually using the Add method.

    Comment

    Working...