Results 1 to 6 of 6

Thread: Populating a ComboBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    89

    Populating a ComboBox

    Hi,

    I have an array of user defined types i.e. newArray(10) how do I populate a ComboBox with this information?

    thanks,

    A Student.

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    basically . . .

    VB Code:
    1. Sub LoadCombo()
    2.     Dim i As Integer
    3.     Combo1.Clear
    4.     For i = 0 To 9
    5.         Combo1.AddItem newArray(i)
    6.     Next i
    7. End Sub

    but you said it was a user defined type and didn't say what part of the variable you wanted to add

    . . . anyway that should get you started
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    89
    My code goes like this:

    [Highlight=VB]
    Private Sub Form_Load()

    Dim i As Integer

    cmbPeople.Clear

    For i= 0 To iTotalPeople 'iTotalPeople is a global variable
    cmbPeople.AddItem (auPeople(iTotalPeople).Name)
    Next i

    End Sub

    This gives me a 'subscript out of range' error.

    Any suggestions?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You need to make sure that the counter in your For loop doesn't pass the upper boundary of the array.

    Something like...
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim i As Integer
    4.  
    5. cmbPeople.Clear
    6.  
    7. For i= 0 To UBound(auPeople) 'UBound returns the upper boundary of the array
    8.     cmbPeople.AddItem (auPeople(i).Name)
    9. Next i
    10.  
    11. End Sub

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    89
    This code only returns one value at a time in the ComboBox. There will be a list however only the name of the last person will appear.

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    What crptcblade was saying you have to remember that your arrays upper bound is not the same as the number of items in the array (iTotalPeople )

    If you have 30 people in the array and iTotalPeople = 30 then your code would fail. The array starts at 0 so the subscripts would be 0 to 29 not 0 to 30.

    try . . .

    VB Code:
    1. For i= 0 To iTotalPeople - 1  'iTotalPeople is a global variable
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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