VB6 Click and Change Events
I am in the middle of creating a booking system. At the moment, I have 2 combo boxes, one for the start time, which in this case is just from 9 to 17, and an option for the length of the lesson, from 1 to 3. What I am trying to accomplish is the following.
If a user selects any start time below 17 (i.e. 16,15, only hourly slots are avaliable) then the combo box for length will display 1, 2 or 3 hours. If they choose 16, then only 1 or 2 hours can be chosen in length as the store closes at 18:00. If 17 is chosen, the combo box should only be populated with 1 hour option.
I have put the code in the combo box Length click event, so when they click on the combo box, the IF statements are conducted, and the box is populated with such and such.
Once the start time and end time are chosen, tStart and tLength respectively, the two integers are added, to produce fTime, which will populate a text box.
However, its not working. Every option selected for the start time, still shows all 1, 2 and 3 hour options in the cmbLength box. Am I using the wrong event, I have also tried it with _change. Any advice is much appreciated.
Code:
Dim tStart As Integer
Dim tLength As Integer
Dim fTime As Integer
Private Sub cmbLength_Click()
tStart = cmbStart.Text
tLength = cmbLength.Text
If tStart = 17 Then
cmbLength.Clear
cmbLength.AddItem "1"
ElseIf tStart = 16 Then
cmbLength.Clear
For Counter = 1 To 2
cmbLength.AddItem Counter
Next Counter
ElseIf tStart < 16 Then
cmbLength.Clear
For Counter = 1 To 3
cmbLength.AddItem Counter
Next Counter
End If
fTime = tStart + tLength
txtEnd.Text = fTime
End Sub
Private Sub Form_Load()
For Counter = 9 To 17
cmbStart.AddItem Counter
Next Counter
For Counter = 1 To 3
cmbLength.AddItem Counter
Next Counter
End Sub
Re: VB6 Click and Change Events
Try this
Code:
Dim tStart As Integer, tLength As Integer, fTime As Integer
Dim Counter As Long
Private Sub Form_Load()
For Counter = 9 To 17
cmbStart.AddItem Counter
Next Counter
End Sub
Private Sub cmbLength_Click()
tStart = Val(cmbStart.Text)
tLength = Val(cmbLength.Text)
cmbLength.Clear
Select Case tStart
Case 17
cmbLength.AddItem "1"
Case 16
cmbLength.AddItem "1"
cmbLength.AddItem "2"
Case Else
For Counter = 1 To 3
cmbLength.AddItem Counter
Next Counter
End Select
fTime = tStart + tLength
txtEnd.Text = fTime
End Sub
Edit: In fact I would suggest the following... Instead of using the click event of the cmbLength, use the change event of cmbStart so your code looks like this...
Code:
Dim tStart As Integer, tLength As Integer, fTime As Integer
Dim Counter As Long
Private Sub Form_Load()
For Counter = 9 To 17
cmbStart.AddItem Counter
Next Counter
End Sub
Private Sub cmbStart_Change()
cmbLength.Clear
Select Case Val(cmbStart.Text)
Case 17
cmbLength.AddItem "1"
Case 16
cmbLength.AddItem "1"
cmbLength.AddItem "2"
Case Else
For Counter = 1 To 3
cmbLength.AddItem Counter
Next Counter
End Select
End Sub
Edit: Marman suggested the same thing :lol: I Didn't refresh before posting... lolzzz
Re: VB6 Click and Change Events
Try putting this in the change event for cmbStart (and removing it from cmbLength:
Code:
If cmbStart.Text = 17 Then
cmbLength.Clear
cmbLength.AddItem "1"
ElseIf tStart = 16 Then
cmbLength.Clear
For Counter = 1 To 2
cmbLength.AddItem Counter
Next Counter
ElseIf tStart < 16 Then
cmbLength.Clear
For Counter = 1 To 3
cmbLength.AddItem Counter
Next Counter
End If
You want to change the length when they set the start time (change cmbStart). I just don't remember if the combo box is updated before or after the event fires and I don't have VB6 here to test it. If it doesn't work you can try the click event of cmdStart.