Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>unsubscribe the event before you set:</p> <pre><code>check1.CheckChanged -= check1_CheckChanged; </code></pre> <p>then you can programmatically set the value without the checkbox firing its CheckChanged event:</p> <pre><code>check1.Checked = true; </code></pre> <p>then re-subscribe:</p> <pre><code>check1.CheckChanged += check1_CheckChanged; </code></pre> <p><strong>[EDIT: March 29, 2012]</strong></p> <p>The problem with Tanvi's approach is you need to catch all source of <strong>manual check or uncheck</strong>. Not that there's too many(it's only from mouse click and from user pressing spacebar), but you have to consider invoking a refactored event from MouseClick and KeyUp(detecting the spacebar)</p> <p>It's more neat for a CheckBox(any control for that matter) to be agnostic of the source of user input(keyboard, mouse, etc), so for this I will just make the programmatic setting of CheckBox really programmatic. For example, you can wrap the programmatic setting of the property to an extension method:</p> <pre><code>static class Helper { public static void SetCheckProgrammatically( this CheckBox c, EventHandler subscribedEvent, bool b) { c.CheckedChanged -= subscribedEvent; // unsubscribe c.Checked = b; c.CheckedChanged += subscribedEvent; // subscribe } } </code></pre> <p>Using this approach, your code can respond neatly to both user's mouse input and keyboard input via one event only, i.e. via CheckChanged. No duplication of code, no need to subscribe to multiple events (e.g. keyboard, checking/unchecking the CheckBox by pressing spacebar)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload