简体   繁体   中英

VB.NET: Multiple events - How to call function only once?

A have a dozen of checkboxes to filter data in DataGridView. They all look more or less so:

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
      filter1=CheckBox1.Checked
      Do_Filter()
    End Sub

I have also a button "Set all":

    Private Sub SetAll_Click(sender As Object, e As EventArgs) Handles SetAll.Click
      CheckBox1.Checked = True
      CheckBox2.Checked = True
      ' etc... 12 times
    End Sub

All works fine. The only problem is that Do_Filter() procedure is called 12 times... Any idea how to postpone Do_Filter() invocation and do it only once at the very end of SetAll_Click() ?

You may introduce a boolean variable:

Private doFilterEnabled As Boolean = True

Add if statement here:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
  filter1=CheckBox1.Checked
  If doFilterEnabled Then
      Do_Filter()
  End If  
End Sub

And small fix here:

Private Sub SetAll_Click(sender As Object, e As EventArgs) Handles SetAll.Click
  doFilterEnabled  = False
  CheckBox1.Checked = True
  CheckBox2.Checked = True
  ' etc... 12 times
  Do_Filter()
  doFilterEnabled  = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM