How can I prevent an assignment statement to Check

2019-09-10 18:44发布

问题:

Given

    Dim cb As CheckBox = New CheckBox
    AddHandler cb, AddressOf cb_CheckChanged
    cb.Checked = True 

...aside from disabling the control, how can I prevent the assignment to Checked from raising the CheckChanged event? I grew up in MFC and events only got raised when the U S E R changed the control's state. What was Softy was thinking? Is it really impossible to distinguish between an event from the user and an event from my own assignment statement? Yikes!

回答1:

it is perfectly valid to raise the event regardless of the thing causing the event because the CheckChanged event just tells you when the Checked property has changed.

If you are trying to avoid the infinite loop you must be experiencing try adding a conditional:

If Not cb.Checked Then
  cb.Checked = True
End If