Search code examples
eventsvb6vertical-scrollingvertical-scroll

Vertical scrollbar change event gets fired twice or more than once even though user click on it once in visual basic


I am working on Visual Basic V6.0 application.

I have a form having vertical scrollbar to change date in calendar and label.

I have attached screenshot of form. When user is clicking only once on highlighted vertical bar arrow - change event gets fired more than once.

I am unable to figure out why is that firing more than once even though we click only once.

I have tried running application in debug more - i don't see any problem.

Also, I have tried putting log msg - which also says - event fired twice. also tried putting flag variable to exit sub if called second time.

This is not occurring every time but yes, most of the time.

Any suggestion is appreciated.

With warmest regards

Vishal Patel

Private Sub Vscroll2_Change()
Dim newmonth As Integer
Dim pass_date As String
Dim curr_dtime As String
Dim yeard As Variant
Dim chg_date As String
Dim set_new_month As Integer


newmonth = VScroll2.Value
curr_dtime = GetFormTagVar(Me, "CURR_DTIME")
Call SetFormTagVar(Me, "OLD_DTIME", curr_dtime)
yeard = Year(curr_dtime)

'set calendar refresh on if we have changed year or month
If (yeard <> Year(curr_dtime)) Or (Month(CVDate(curr_dtime)) <> newmonth) Then
   SetCalRefresh (True)
End If

If Month(CVDate(curr_dtime)) <> newmonth Then
   set_new_month = False
   If newmonth = 13 Then
      newmonth = 1
      set_new_month = True
      yeard = Year(curr_dtime)
      yeard = yeard + 1
   End If

   If newmonth = 0 Then
      newmonth = 12
      set_new_month = True
      yeard = Year(curr_dtime)
      yeard = yeard - 1
   End If

   '* figure out the new date
   If newmonth < 10 Then
       pass_date = yeard & "0" & newmonth & "01" & "0000"
   Else
       pass_date = yeard & newmonth & "01" & "0000"
   End If
   pass_date = DatePaint(pass_date, True)

   Call SetFormTagVar(Me, "CURR_DTIME", pass_date)
   Call SetFormTagVar(Me, "NEW_DTIME", "1")
   lbldate.Caption = DatePaint(DateParseFnc(pass_date), True)
   chg_date = GetFormTagVar(Me, "CURR_DTIME")
   If set_new_month Then
    Call SetFormTagVar(Me, "NEW_MONTH", YES_FLAG)
    VScroll2.Value = newmonth
   End If
   Call check_calendar(Me)
   Call SetupNotesMenuItems
End If

'We're done
End Sub

enter image description here


Solution

  • If set_new_month Then
        Call SetFormTagVar(Me, "NEW_MONTH", YES_FLAG)
        VScroll2.Value = newmonth
    End If
    

    Here you set the value of the scroll bar from within the _Change event. This causes another _Change to fire. When you do this you get the situation you describe. The usual solution is to surround the change with a boolean that indicates that a change is already happening.