0

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

Vishal Patel
  • 41
  • 4
  • 9

1 Answers1

1
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.

Jim Mack
  • 1,070
  • 1
  • 7
  • 16
  • Sorry for the late response. Thanks @Jim Mack. I'll try out that. – Vishal Patel Jan 30 '17 at 04:06
  • Hi @Jim Mack, I have tried putting below code to test as per your recommendation. `If we = True Then we = False Exit Sub End If` before reaching to VScroll2.Value=newmonth i have put we=true – Vishal Patel Jan 30 '17 at 05:51
  • **and I saw, change event gets fired only when mouse is hovered on scrollbar. Click once and move mouse to some-other control or area of form is not causing any issue/calling change event of scrollbar.** – Vishal Patel Jan 30 '17 at 05:55
  • **adding to comment number 2.** this solution is not helping to resolve issue i tried removing that section itself of changing value of scrollbar to newmonth - still same issue – Vishal Patel Jan 30 '17 at 05:57
  • @VishalPatel - Is your boolean declared as Static in the procedure? If you place the trap at the very beginning of the Sub it *cannot* be entered while still active. `Static bHere as boolean // if bHere then exit sub // bHere = true //.... main contents.... // bHere = false` Try that and tell us what you see. – Jim Mack Jan 30 '17 at 12:51
  • I have tried putting same code. It did not work. I saw log - logically we are fine with that code. BUT somehow mousehover is calling change event again. – Vishal Patel Jan 31 '17 at 05:37
  • @VishalPatel - There is no mechanism in VB6 for a mouseover to cause a scroll bar value change. Is this possibly related to Windows Accessibility settings? – Jim Mack Jan 31 '17 at 21:32
  • I don't think so. This i have tried in diff diff systems. same problem. – Vishal Patel Feb 02 '17 at 11:56