-
Mar 25th, 2015, 10:40 PM
#1
Thread Starter
Lively Member
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?
-
Mar 25th, 2015, 11:51 PM
#2
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.
-
Mar 26th, 2015, 08:12 AM
#3
Thread Starter
Lively Member
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
-
Mar 26th, 2015, 09:08 AM
#4
Re: assigning arrays to a list box
Originally Posted by GetOverHere89
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.
-
Mar 26th, 2015, 09:38 AM
#5
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
-
Mar 26th, 2015, 09:42 AM
#6
Thread Starter
Lively Member
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?
-
Mar 26th, 2015, 09:47 AM
#7
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
-
Mar 26th, 2015, 10:01 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|