即使用户在 visual basic 中单击一次垂直滚动条更改事件也会被触发两次或多次

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

我正在开发 Visual Basic V6.0 应用程序。

我有一个带有垂直滚动条的表单,用于更改日历和标签中的日期。

我附上了表格的截图。当用户仅在突出显示的垂直条箭头上单击一次时 - 更改事件会被触发不止一次。

我无法弄清楚为什么即使我们只点击了一次也触发了不止一次。

我已经尝试 运行 调试更多应用程序 - 我没有发现任何问题。

另外,我试过将日志消息 - 这也表示 - 事件触发了两次。 如果第二次调用,还尝试将标志变量放入退出 sub。

这不是每次都会发生,但大多数时候都会发生。

如有任何建议,我们将不胜感激。

谨致问候

维沙尔·帕特尔

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

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

在这里,您可以在 _Change 事件中设置滚动条的值。这会导致另一个 _Change 触发。当你这样做时,你会得到你描述的情况。通常的解决方案是用一个布尔值包围更改,表示更改已经发生。