简体   繁体   中英

how to populate listbox with array items

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM