|
-
May 19th, 2003, 10:39 PM
#1
Thread Starter
Fanatic Member
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:
Option Explicit
Dim lStage
Dim lStageEnd
Private Sub Form_Load()
' Example start value.
lStage = 7
With Combo1
.AddItem "0"
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
.AddItem "5"
.AddItem "6"
.AddItem "7"
.AddItem "8"
.AddItem "9"
End With
Combo1.ListIndex = lStage
Label1.Caption = lStage
Combo2.Clear
With Combo2
If lStage <= 0 Then .AddItem "0"
If lStage <= 1 Then .AddItem "1"
If lStage <= 2 Then .AddItem "2"
If lStage <= 3 Then .AddItem "3"
If lStage <= 4 Then .AddItem "4"
If lStage <= 5 Then .AddItem "5"
If lStage <= 6 Then .AddItem "6"
If lStage <= 7 Then .AddItem "7"
If lStage <= 8 Then .AddItem "8"
If lStage <= 9 Then .AddItem "9"
End With
lStageEnd = Combo2.ListCount - 1
Combo2.ListIndex = lStageEnd
Label2.Caption = lStageEnd
End Sub
Private Sub Combo1_Click()
Dim lStageOld As Integer
lStageOld = lStage
lStage = Combo1.ListIndex
Label1.Caption = lStage
Combo2.Clear
With Combo2
If lStage <= 0 Then .AddItem "0"
If lStage <= 1 Then .AddItem "1"
If lStage <= 2 Then .AddItem "2"
If lStage <= 3 Then .AddItem "3"
If lStage <= 4 Then .AddItem "4"
If lStage <= 5 Then .AddItem "5"
If lStage <= 6 Then .AddItem "6"
If lStage <= 7 Then .AddItem "7"
If lStage <= 8 Then .AddItem "8"
If lStage <= 9 Then .AddItem "9"
End With
If lStageEnd <= lStage Then
Combo2.ListIndex = 0
Else
Combo2.ListIndex = lStageEnd + (lStage - lStageOld)
End If
End Sub
Private Sub Combo2_Click()
lStageEnd = Combo1.ListIndex + Combo2.ListIndex
Label2.Caption = lStageEnd
End Sub
Last edited by WorkHorse; May 20th, 2003 at 08:39 PM.
-
May 19th, 2003, 11:11 PM
#2
Fanatic Member
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:
' Example start value.
lStage = 7
With Combo1
For i = 0 To 9
.AddItem i
Next i
End With
Combo1.ListIndex = lStage
Label1.Caption = lStage
Combo2.Clear
With Combo2
For i = lStage To 9
.AddItem i
Next i
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!
-
May 19th, 2003, 11:33 PM
#3
Thread Starter
Fanatic Member
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.
-
May 20th, 2003, 08:38 PM
#4
Thread Starter
Fanatic Member
I worked it out. Pretty simple really. I think my brain just wasn't working yesterday.
VB Code:
Option Explicit
Dim lStage
Dim lStageEnd
Private Sub Form_Load()
Dim i As Integer
' Example start values.
lStage = 4
lStageEnd = 6
For i = 0 To 9
Combo1.AddItem i
Next
Combo1.ListIndex = lStage
UpdateCombo2
End Sub
Private Sub Combo1_Click()
lStage = Combo1.ListIndex
UpdateCombo2
End Sub
Private Sub Combo2_Click()
lStageEnd = Combo2.ListIndex + Combo1.ListIndex
End Sub
Private Sub UpdateCombo2()
Dim i As Integer
Combo2.Clear
For i = 0 To 9
If Combo1.ListIndex <= i Then
Combo2.AddItem i
End If
Next
If Combo1.ListIndex > lStageEnd Then
lStageEnd = Combo1.ListIndex
End If
Combo2.ListIndex = lStageEnd - Combo1.ListIndex
End Sub
-
May 20th, 2003, 10:49 PM
#5
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|