+ Reply to Thread
Results 1 to 5 of 5

can't select ListBoxB items (or values) based on index (& values) of ListBoxA

  1. #1
    Registered User
    Join Date
    05-24-2004
    Posts
    52

    can't select ListBoxB items (or values) based on index (& values) of ListBoxA

    Hi,

    I've got a userform that fills ListBox1 with the contents of cells in Row1 (=column headers). When the user selects items from it (multi-select=true), each of the selected items are copied into ListBox3 & ListBox4. The user can then select one of THOSE items from ListBox4 which is then saved as a string variable, 'DepVar'. Lastly, the user selects one or more other items from ListBox3 which are then fed into an array, BlossomIndepVarArr(), for use later on.

    The problem I'm having is that I'd like the items in ListBoxes 3 & 4 to refer directly back to the order/index of items in ListBox1. For example, if I select items 3-6 (but not 1-2 or 7-10) from ListBox1, those three items would be copied into both ListBox3 & ListBox4; then, if I selected the 2nd item from ListBox3 and the 4th item from ListBox4, the code should recognise them as ListBox1's item 4 and item 6, respectively -- and NOT as 2 & 4.

    I've tried using all sorts of arrays and counters to get the code to recognise the selected headers/items from all 3 listboxes, but none of my modifications have worked ALL of the time -- i.e. they screw up once I change the combination/pattern of selected items from ListBox1.
    Here's the code most consistent (but still incorrect) version of the code. I'd love to know if anyone can see why it isn't working:
    Please Login or Register  to view this content.
    Can you see what's going wrong?? Can you suggest a solution that'll work no matter which ListBox1 items are picked, even if items are skipped in between selections? I just can't seem to get this to work, so a second opinion would be most welcome...
    Thanks so much, F

  2. #2
    Registered User
    Join Date
    05-24-2004
    Posts
    52

    lost in the shuffle...

    Isn't there anyone out there who can help me??

  3. #3
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282
    fern,

    I've been really busy and haven't looked at the forum in a while...I'll take a look at this and see if I can help you out.

    theDude

  4. #4
    Forum Contributor
    Join Date
    11-16-2004
    Posts
    282
    F,

    From Visual Basic Help…

    The ListIndex property contains an index of the selected row in a list. Values of ListIndex range from –1 to one less than the total number of rows in a list (that is, ListCount – 1). When no rows are selected, ListIndex returns –1. When the user selects a row in a ListBox or ComboBox, the system sets the ListIndex value. The ListIndex value of the first row in a list is 0, the value of the second row is 1, and so on.
    Hopefully I can explain this so it’s clear…Unfortunately, you can’t use the ListIndex property of ListBox1 because ListBox1 allows multiple selections. In a ListBox that allows multiple selections, ListIndex returns the index of the row that has focus, regardless of whether that row is currently selected.

    On the bright side, think of a ListBox list like an array that always has an Option Base of zero (it’s not negotiable). You can get the ‘index’ of the selected item(s) from the list in ListBox1 to populate the array ‘SelArr’ by using the value of your variable ‘i’as the ‘index’:
    Please Login or Register  to view this content.
    So, from your example above:
    “if I select items 3-6 (but not 1-2 or 7-10) from ListBox1”
    After ‘SelArr’ is loaded, its’ data should look like this:
    SelArr (the list ‘index’ number of item(s) selected from ListBox1)
    1 (Automatically selected the 2nd item from ListBox1 and represents List item 2)
    2 (Represents List item 3 that was selected)
    3 (Represents List item 4 that was selected)
    4 (Represents List item 5 that was selected)
    5 (Represents List item 6 that was selected)

    Your code will then populate ListBox3 & ListBox4 identically like this:
    ListBox3&4 List Text
    ListItem 2.Text (Index number = 0; auto-selected the 2nd item from ListBox1)
    ListItem 3.Text (Index number = 1)
    ListItem 4.Text (Index number = 2)
    ListItem 5.Text (Index number = 3)
    ListItem 6.Text (Index number = 4)

    You can then retrieve the selected items’ ‘index’ in ListBox3 and ListBox4 using the same methodology above if they are ‘multi-select’; if not, use the ListIndex property and add 1 to the ‘index’ (it appears ‘SelArr’ is Option Base 1):
    Please Login or Register  to view this content.
    Then loop through ‘SelArr’ records to find the matching original ‘index’ number from ListBox1:
    Please Login or Register  to view this content.
    Hope this helps,
    theDude

  5. #5
    Registered User
    Join Date
    05-24-2004
    Posts
    52

    Wow

    Wow, that fantastic theDude! Thank you so much, yet again... I'd given up on getting a reply ages ago and had cobbled together a similar, though less effective solution on my own instead - but I can already see that your version will be both a big improvement over mine and a relatively easy one to impliment given what I've already got.
    I'm really grateful that you took time out of your busy schedule to not only help me solve my problem, but also to explain how & why it works. That'll be a huge help if I get stuck again later on (or if anyone else hits the same wall).
    Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1