简体   繁体   中英

how to populate listbox with array items 2

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

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

As Steven Doggart said, you are probably going about this the wrong way. The first part should probably look something like this:

For i As Integer = 1 To 10 Step 1
  Dim newDefinition As String = 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)
Next

That assumes that you always want the user to enter 10 items. After they have done that, then you do whatever it is that you want to do with those 10 items.

Dim newDefinitions As IEnumerable(Of String) = (From item In lstDefinitions.Items
                                                Select item.ToString())

For Each newDef As String in newDefinitions
    ' Do something with each string here
    Console.Writeline(newDef)
    ' I wrote to Console, but you can add them to another listbox or whatever.
Next

The first step that you describe is adding the items that are entered via InputBox into a ListBox . That can be accomplished by calling the Add method on the ListBox , like this:

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)

However, that only adds one item. If you want them to add multiple, you'll need to put that code in a separate event handler, such as the Click event of an "Add Definition" button. If you want to force them to add a fixed number of them sequentially, you could instead use a loop, for instance:

For i As Integer = 0 to 9
    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)
Next

Once you have all of the items in a ListBox , reading all of the items out of it to store them in an array is fairly easy. You can do it with LINQ, like this:

Dim items() As String = lstDefinitions.Items.OfType(Of String).ToArray()

Or you can use a loop, like this:

Dim items(lstDefinitions.Items.Count - 1) As String  ' Create the array with the correct size
For i As Integer = 0 To lstDefinitions.Items.Count   ' Loop through all of the items in the list box
    items(i) = lstDefinitions.Items(i).ToString()    ' Add the current item to the array
Next

Once you have the array, it's very easy to add them to the second ListBox , like this:

ListBox2.Items.AddRange(items)

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