Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign an Array to a Combo Box - HELP

Status
Not open for further replies.
Apr 18, 2002
185
US
I have a combo box with 3 sizes in it (small, medium, large) and each of them have a price associated with them ($7.00, $9.00, $11.00 - respectively).

I want to create an array, I think, so that I can select one and get the amount...

How do I do this?
 
This should auto create your array and auto populate your array...should, it may need to be tweaked a bit

Dim i as Integer
i = 0
Dim Countries(nameOfComboBox.ListCount) As String 'dynamically creates an array depending on how many items are in the combobox

DO while i <= nameOfComboBox.ListCount
Countries(i) = formName.nameOfComboBox.List(i)
i = i + 1
Loop

FYI: ListCount provides a number of how many items are in your combobox

FYI: List(integer) provides you the text value of that item

For additional help, go here:
 
You would have to subtract 1 from the ListCount value, otherwise the code will throw an exception at the last value of i (base 0 rule).
But any way, that doesn't solve the problem. You are left with a String array with the combobox's values. You would be better off using a structure:

Code:
'The structure:
Public Structure PriceInfo
  Public Size As String
  Public Price As Integer
End Structure

'The array to hold the various possible combinations:
Private PriceInfos(2) As PriceInfo

'Populate the array with its values on load:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  PriceInfos(0).Size = "Small"
  PriceInfos(0).Price = 7
  PriceInfos(1).Size = "Medium"
  PriceInfos(1).Price = 9
  PriceInfos(2).Size = "Large"
  PriceInfos(2).Price = 11
End Sub

'Handle the event of the combobox selected index changed:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  Dim currentSize As String = ComboBox1.Text
  For i As Integer = 0 To 2
    If PriceInfos(i).Size = currentSize Then
      MsgBox(PriceInfos(i).Price)
      Exit For
    End If
  Next
End Sub

You should make sure that the DropDownStyle property of the combobox is set to DropDownList, in order to prevent a user entering an invalid value for the size.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top