Results 1 to 8 of 8

Thread: assigning arrays to a list box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    assigning arrays to a list box

    I have 2 arrays, a list box, and a label to display the output. I need the user to be able to click the name in the list box and then when the display button is clicked, the label will display a corresponding number. Obviously the two arrays are one for names, one for numbers. What would I need to do to get the names from the names array to display in the list box and have them correspond in parallel to the array?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: assigning arrays to a list box

    You can either assign the first array to the DataSource property of the ListBox or pass it to a call to Items.AddRange. You can then handle the SelectedIndexChanged event, get the SelectedIndex of the control and use that as an index into the second array to get the corresponding number, which you then display in the Label.

    There is a better way to do all that though. If you have specific instructions to use concurrent arrays then that's what you have to do but, in a real-world application, you'd only use one list. You would first define a type that had two properties: one for a name and one for a number. You would then create instances of that type and add them to a collection or array. You'd then bind that list to the ListBox via a BindingSource. You'd bind the Label to the same BindingSource and the Label would update automatically.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: assigning arrays to a list box

    Yeah we have to use 2 arrays. I thought the easiest way would be to just do a parallel array and listbox. So if I use the Items.AddRange would it be something like

    Dim strArrayNames = {enter 9 names here}
    Dim intCount As Integer

    For intCount =0 To strArrayNames.Length-1
    strArrayNames (intCount) = lstNames.Items.Add
    Next

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: assigning arrays to a list box

    Quote Originally Posted by GetOverHere89 View Post
    Yeah we have to use 2 arrays. I thought the easiest way would be to just do a parallel array and listbox. So if I use the Items.AddRange would it be something like

    Dim strArrayNames = {enter 9 names here}
    Dim intCount As Integer

    For intCount =0 To strArrayNames.Length-1
    strArrayNames (intCount) = lstNames.Items.Add
    Next
    AddRange is a single method call to add a range of items. There's no loop required.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: assigning arrays to a list box

    The first step that you would need to do is to declare and initialize your arrays:
    Code:
    Private names() As String
    Private numbers() As Integer
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.OnLoad()
    	names = {"David", "Marie", "Michael", "Paige", "Christopher", "Sharon", "Dexter", "Rebecca", "Edward"}
    	numbers = {12, 8, 10, 1, 55, 73, 14, -8, 4}
    
    
    End Sub
    Once the arrays are filled, you will need to populate your ListBox with one of the arrays, presumably the array representing the names:
    Code:
    Private names() As String
    Private numbers() As Integer
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.OnLoad()
    	names = {"David", "Marie", "Michael", "Paige", "Christopher", "Sharon", "Dexter", "Rebecca", "Edward"}
    	numbers = {12, 8, 10, 1, 55, 73, 14, -8, 4}
    
    	NamesListBox.Items.AddRange(names)
    End Sub
    Now that the names are populated in the ListBox, the next step will be to display the corresponding number in the label once the button has been clicked. To do this, get the SelectedIndex property of your ListBox to get the item's index in the numbers array:
    Code:
    Private Sub NumberButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles NumberButton.Click()
    	NumberLabel.Text = numbers(NamesListBox.SelectedIndex)
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: assigning arrays to a list box

    So it would just be

    strArrayNames() = lstNames.Items.AddRange

    Will that automatically assign the names from the arrays to a parallel index in the list box? And then if I make another parallel array to the original array, it will correspond when the user clicks the selection in the list box?

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: assigning arrays to a list box

    No, in the example that I gave I show how AddRange should be used. The code that you presented assigns your strArrayNames variable to your ListBox's Items's AddRange method which shouldn't even allow the code to compile. However, whenever in doubt you should look at the documentation on MSDN: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: assigning arrays to a list box

    Sorry I didn't see your first post before I replied. Thank you for your help. I've never used Arrays before and I've barely used list boxes. This was a huge help!

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