Search code examples
winformscheckboxoncheckedchanged

How to distinguish user vs programmatic changes in WinForms CheckBox?


I have logic on a CheckBox's OnCheckedChanged event that fires on form load as well as when user changes check state. I want the logic to only execute upon user action.

Is there a slick way of detecting user vs programmatic change that doesn't rely on setting/checking user variables?


Solution

  • I usually have a bool flag on my form that I set to true before programmatically changing values. Then the event handler can check that flag to see if it is a user or programmatic.