Eli Sokal
Eli Sokal

Reputation: 31

How can I prevent an assignment statement to CheckBox.Checked from raising the CheckChanged event?

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!

Upvotes: 0

Views: 106

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

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

Upvotes: 2

Related Questions