Results 1 to 14 of 14

Thread: Reading data items into listbox

  1. #1

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105

    Question Reading data items into listbox

    Just a quick question.

    I want to read a range of 2 digit numbers into a Combolistbox. I could put them into a string i.e. datastring = "12345678........" and then read 2 digits from the left in position steps of 2. Would using the 'Print Mid' function here work?

    Is there a better way of doing this? can I separate the individual number pairs with a comma and 'read' them someway into a Combolistbox.additem method?

    Any comments and suggestions welcome.

    Thanks.
    Old divers never die, they just go down on old wrecks

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Where are the numbers coming from?

    If your gonna do the string approach, use a delimeter. Then use the Split() function on the string, you'll get an array from that. You can then iterate through the array, adding the array elements to the list.

  3. #3

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105
    The numbers are fixed values obtained when a user selects an option from a first Listbox.

    So, depending on what the user selects from the first Listbox, a group of numbers are slected and these are used to create the contents of a second Listbox.
    Old divers never die, they just go down on old wrecks

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Are the values in the first list box static? Will they always put the same numbers in the second list box?

    If so, just do a simple routine to place those numbers in the second list box when that option is chosen in the first one.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    So the first listbox contains numeric values? Or we're you refering to the index range of the selected values in the first listbox?

    If so then iterate through the items of the first listbox. If List1.Selected = True then add the List1.ListIndex to the second listbox.

  6. #6

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105
    To elaborate.

    Listbox 1 has fixed options: MenuItem1, menuItem2.... MenuItem11 etc.. These are fixed options.

    When the user selects say MenuItem1, using the Case Select statement a number of fixed numbers (10, 12, 14, 16, ....45) are used to create the MenuItems for a second list box.

    So, MenuItem1 = 10, 12, 14, 16, 20 ........ 45
    Menuitem 2 = 18, 20, 20 ....... 40
    .
    .
    MenuItem11 = 20, 30, 40 ....................

    etc.

    All these numbers and menuitems are fixed, nothing will ever vary.
    Old divers never die, they just go down on old wrecks

  7. #7
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Then in that Case statement, add those numbers to the second listbox with AddItem.

    You also mixed 2 controls in the same sentance in your first post. You said a ComboListBox.

    Is it a ComboBox or a ListBox for the 2nd one?

  8. #8

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105
    Then if I have 11 MenuItems in Listbox1 I am going to have 11 AddMenu.item routines to generate the second Listbox. If I can generate a String from any of the 11 options I just need to have 1 AddMenu.item routine to populate the second Listbox. i.e. use a loop: For i = 1 to len(MenuItemString) Next.

    Although this is quite a straightforward thing to do, I want to use the minimum of code as I will have a large number of similar operations to set up Multiple Listboxes.

    Regards.
    Old divers never die, they just go down on old wrecks

  9. #9
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Then generate a comma delimited string and use split and read it into an array. Then add each item from the array.

  10. #10

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105
    Sorry Brian, I didn't answer one of your questions. These are Combo boxes selected as style 2 (dropdown lists).
    Old divers never die, they just go down on old wrecks

  11. #11

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105
    I wasn't aware of the Split function. This possibly will do the trick, I will check it out later.

    Thanks for your comments guys, much appreciated.

    Regards.
    Old divers never die, they just go down on old wrecks

  12. #12
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Pass the comma delimeted string (or the array after split) as a parameter for the procedure that will populate the second listbox.

  13. #13
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Example

    VB Code:
    1. Dim mystring As String
    2. Dim myarray() As String
    3. Dim x As Integer
    4.  
    5. mystring = "12,34,56,78,90"
    6.  
    7. myarray = Split(mystring, ",")
    8.  
    9. For x = 0 To UBound(myarray, 1)
    10.  
    11.     Combo1.AddItem myarray(x)
    12.    
    13. Next x

    That will load 12, 34, 56, 78 and 90 as items in the combo1 box.

  14. #14

    Thread Starter
    Lively Member Steve Cain's Avatar
    Join Date
    Sep 2000
    Location
    Wiltshire, UK
    Posts
    105

    Thumbs up

    Thanks for your help guys. Using the Split function worked a treat and did exactly as I wanted.

    Regards.
    Old divers never die, they just go down on old wrecks

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