Results 1 to 5 of 5

Thread: Combo Combo [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    Combo Combo [RESOLVED]

    For some reason I can't work this out. I have two combo boxes. The first Combo1 lists all options (there are 10 options). The second Combo2 lists the "end option". The program will process all records from Combo1 to Combo2. It's a basic sort of "For x= Combo1 to Combo2" type deal. So Combo2 must always be equal or greater than or equal to Combo1, and the Combo2 list only includes values greater than or equal to Combo1 selection.

    So: If User selects Combo1 value greater than Combo2 value, then Combo2 value is same as Combo1. If User selects Combo1 less than than Combo2 value, then Combo2 value stays the same, but the Combo2 list expands to include all option of Combo1 or greater and adjusts the Combo2 selection. Do you follow? Combo2 is a list of all Combo1 selection and higher. Combo2 selection stays the same unless Combo1 selection is higher.

    Here's what I have:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim lStage
    4. Dim lStageEnd
    5.  
    6. Private Sub Form_Load()
    7.  
    8.     ' Example start value.
    9.     lStage = 7
    10.  
    11.     With Combo1
    12.         .AddItem "0"
    13.         .AddItem "1"
    14.         .AddItem "2"
    15.         .AddItem "3"
    16.         .AddItem "4"
    17.         .AddItem "5"
    18.         .AddItem "6"
    19.         .AddItem "7"
    20.         .AddItem "8"
    21.         .AddItem "9"
    22.     End With
    23.     Combo1.ListIndex = lStage
    24.     Label1.Caption = lStage
    25.    
    26.     Combo2.Clear
    27.     With Combo2
    28.         If lStage <= 0 Then .AddItem "0"
    29.         If lStage <= 1 Then .AddItem "1"
    30.         If lStage <= 2 Then .AddItem "2"
    31.         If lStage <= 3 Then .AddItem "3"
    32.         If lStage <= 4 Then .AddItem "4"
    33.         If lStage <= 5 Then .AddItem "5"
    34.         If lStage <= 6 Then .AddItem "6"
    35.         If lStage <= 7 Then .AddItem "7"
    36.         If lStage <= 8 Then .AddItem "8"
    37.         If lStage <= 9 Then .AddItem "9"
    38.     End With
    39.  
    40.     lStageEnd = Combo2.ListCount - 1
    41.     Combo2.ListIndex = lStageEnd
    42.     Label2.Caption = lStageEnd
    43.  
    44. End Sub
    45.  
    46. Private Sub Combo1_Click()
    47.  
    48.     Dim lStageOld As Integer
    49.    
    50.     lStageOld = lStage
    51.    
    52.     lStage = Combo1.ListIndex
    53.     Label1.Caption = lStage
    54.    
    55.     Combo2.Clear
    56.     With Combo2
    57.         If lStage <= 0 Then .AddItem "0"
    58.         If lStage <= 1 Then .AddItem "1"
    59.         If lStage <= 2 Then .AddItem "2"
    60.         If lStage <= 3 Then .AddItem "3"
    61.         If lStage <= 4 Then .AddItem "4"
    62.         If lStage <= 5 Then .AddItem "5"
    63.         If lStage <= 6 Then .AddItem "6"
    64.         If lStage <= 7 Then .AddItem "7"
    65.         If lStage <= 8 Then .AddItem "8"
    66.         If lStage <= 9 Then .AddItem "9"
    67.     End With
    68.    
    69.     If lStageEnd <= lStage Then
    70.         Combo2.ListIndex = 0
    71.     Else
    72.         Combo2.ListIndex = lStageEnd + (lStage - lStageOld)
    73.     End If
    74.  
    75. End Sub
    76.  
    77. Private Sub Combo2_Click()
    78.     lStageEnd = Combo1.ListIndex + Combo2.ListIndex
    79.     Label2.Caption = lStageEnd
    80. End Sub
    Last edited by WorkHorse; May 20th, 2003 at 08:39 PM.

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I'm still not completely sure what you want to do, but right here

    lStageEnd = Combo1.ListIndex + Combo2.ListIndex

    you set the lStageEnd to a value that causes

    Combo2.ListIndex = lStageEnd + (lStage - lStageOld)

    to crash


    You combo1 and combo2 click events both fire when your form loads. Setting the listindex of a combo box fires it's click event. This in turn is changing your values (earlier than I think you expected) and causing you to reference a listindex that doesn't exist.

    Did that make any sense?

    Also you could simplify your code with . . .

    VB Code:
    1. ' Example start value.
    2.     lStage = 7
    3.     With Combo1
    4.         For i = 0 To 9
    5.             .AddItem i
    6.         Next i
    7.     End With
    8.     Combo1.ListIndex = lStage
    9.     Label1.Caption = lStage
    10.    
    11.     Combo2.Clear
    12.     With Combo2
    13.         For i = lStage To 9
    14.             .AddItem i
    15.         Next i
    16.     End With

    that would make it quite a bit eaiser to load the combos.
    "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
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Thanks. I can't really simplfy it. The combos will have pre-set values. I'm just using 0-9 to make it easy to work out the problems. What I'm trying to do is:

    1) Combo2 list is Combo1 value to end of list
    2) If Combo2 > Combo1 then Combo2 value = Combo1 value
    3) Else Combo2 value stays the same (but the Combo2 index is different because the number of items in Combo2 has changed.

    For example:

    lStage = 5: lStageEnd = 8

    So:

    Combo1.Index = 5
    Combo2 list is 5-9.
    Combo2.Index = 4

    If User changes Combo1 = 3 then

    lStage = 3
    Still lStageEnd = 8
    Combo2 list is 3-9
    but now Combo2.Index must be 6.

    So, the problem is that the index in Combo2 changes based on the value of Combo1.

  4. #4

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I worked it out. Pretty simple really. I think my brain just wasn't working yesterday.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim lStage
    4. Dim lStageEnd
    5.  
    6. Private Sub Form_Load()
    7.  
    8.     Dim i As Integer
    9.  
    10.     ' Example start values.
    11.     lStage = 4
    12.     lStageEnd = 6
    13.  
    14.     For i = 0 To 9
    15.         Combo1.AddItem i
    16.     Next
    17.     Combo1.ListIndex = lStage
    18.    
    19.     UpdateCombo2
    20.    
    21. End Sub
    22.  
    23. Private Sub Combo1_Click()
    24.     lStage = Combo1.ListIndex
    25.     UpdateCombo2
    26. End Sub
    27.  
    28. Private Sub Combo2_Click()
    29.     lStageEnd = Combo2.ListIndex + Combo1.ListIndex
    30. End Sub
    31.  
    32. Private Sub UpdateCombo2()
    33.  
    34.     Dim i As Integer
    35.  
    36.     Combo2.Clear
    37.     For i = 0 To 9
    38.         If Combo1.ListIndex <= i Then
    39.             Combo2.AddItem i
    40.         End If
    41.     Next
    42.    
    43.     If Combo1.ListIndex > lStageEnd Then
    44.         lStageEnd = Combo1.ListIndex
    45.     End If
    46.    
    47.     Combo2.ListIndex = lStageEnd - Combo1.ListIndex
    48.  
    49. End Sub

  5. #5
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by WorkHorse
    I worked it out. Pretty simple really. I think my brain just wasn't working yesterday.
    Good to hear! Sorry I didn't get back again sooner. I was gonna try to whip up something, but it looks like you already did.
    "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