Results 1 to 3 of 3

Thread: VB6 Click and Change Events

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2010
    Posts
    27

    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

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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 I Didn't refresh before posting... lolzzz
    Last edited by Siddharth Rout; Oct 20th, 2010 at 06:53 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    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.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

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