0

Im trying to populate a listbox with items inside the array, i have declared an array and assigned strings which it holds but im not sure if i even done that correctly, i want to use that strings that are in the array to populate a list box, here is the code i have already done, how do i do that, could anyone give me code that i could use to populate listbox with those strings.

Dim NewDefinition As String

    NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")

    lstDefinitions.Items.Add(NewDefinition)

    Dim NewDefinition1 As String = lstDefinitions.Items(0).ToString
    Dim NewDefinition2 As String = lstDefinitions.Items(1).ToString
    Dim NewDefinition3 As String = lstDefinitions.Items(2).ToString
    Dim NewDefinition4 As String = lstDefinitions.Items(3).ToString
    Dim NewDefinition5 As String = lstDefinitions.Items(4).ToString
    Dim NewDefinition6 As String = lstDefinitions.Items(5).ToString
    Dim NewDefinition7 As String = lstDefinitions.Items(6).ToString
    Dim NewDefinition8 As String = lstDefinitions.Items(7).ToString
    Dim NewDefinition9 As String = lstDefinitions.Items(8).ToString
    Dim NewDefinition10 As String = lstDefinitions.Items(9).ToString

    Dim NewDefinitions(10) As String
    NewDefinitions(0) = NewDefinition1
    NewDefinitions(1) = NewDefinition2
    NewDefinitions(2) = NewDefinition3
    NewDefinitions(3) = NewDefinition4
    NewDefinitions(4) = NewDefinition5
    NewDefinitions(5) = NewDefinition6
    NewDefinitions(6) = NewDefinition7
    NewDefinitions(7) = NewDefinition8
    NewDefinitions(8) = NewDefinition9
    NewDefinitions(9) = NewDefinition10
2
  • 3
    Your question is confusing. You are asking how to add an array to a list box, but your code example shows how you are reading items out of a list box into an array. Are you wanting to do that but then add the items from the array to a second list box? Commented Mar 25, 2014 at 14:02
  • Intially i want to make it work so there is one listbox on one form in my program, somone inputs 10 texts using the inputbox, then each text he entered is a single variable string, those strings are then assigned to an array and this array is used to populate another listbox on different form, i though it would be easier that way, i have very limited knowledge on arrays i must to admit, i think i made it more clearer @StevenDoggart Commented Mar 25, 2014 at 16:59

1 Answer 1

1

Listbox will accept any object and will display whatever the objects ToString method will display. Therefore you can populate the listbox with the objects directly. See if this works for you:

ListBox1.DataSource = lstDefinitions.Items

If the objects in question are from a custom class you can override the ToString method to display the information you desire.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.