Programmatically click on a CheckBox

29,711

Solution 1

Why do you need to simulate a click, doesn't this line of code fits your need?

myCheckBox.Checked = !myCheckBox.Checked;

If you need to execute logic when the state of the CheckBox changes, you should use CheckedChanged event instead of Click.

private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
   MessageBox.Show("You are in the CheckBox.CheckedChanged event.");
}

Solution 2

Why do you want to generate a click event on the CheckBox?

If you want to toggle it's value:

theCheckBox.Checked = !theCheckBox.Checked;

If you want to trigger some functionality that is connected to the Click event, it's a better idea to move the code out from the Click event handler into a separate method that can be called from anywhere:

private void theCheckBox_Click(object sender, EventArgs e)
{
    HandleCheckBoxClick((CheckBox)sender);
}

private void HandleCheckBoxClick(CheckBox sender)
{
    // do what is needed here
}

When you design your code like that, you can easily invoke the functionality from anywhere:

HandleCheckBoxClick(theCheckBox);

The same approach can (and perhaps should) be used for most control event handlers; move as much code as possible out from event handlers and into methods that are more reusable.

Solution 3

Those solutions above calls Checkbox.CheckedChanged event.

If you want to explicitly call Click event you can this:

checkBox1_Click(checkBox1, null);

Solution 4

I'm still setting up a new workstation so I can't research this properly at the moment, but with UI Automation maybe it's possible that the checkbox supports the IInvokeProvider and you can use the Invoke method?

Share:
29,711
Grzenio
Author by

Grzenio

Software Engineer at Google

Updated on July 09, 2022

Comments

  • Grzenio
    Grzenio almost 2 years

    Is there a way to programmatically generate a click event on a CheckBox? I am looking for an equivalent to Button.PerformClick();

  • Matt Breckon
    Matt Breckon over 14 years
    I posted this but the question says "click event". This won't generate a click event.
  • Grzenio
    Grzenio over 14 years
    I am trying to tests a nasty Form, that enables some of the containing controls in the ..._Click event handler.
  • Philip Wallace
    Philip Wallace over 14 years
    Just be careful - you don't want to call this method from theCheckBox_Click AND theCheckBox_CheckedChanged as it will be called twice when the check box is clicked.
  • Grzenio
    Grzenio over 14 years
    Maybe I will refactor the control and use the CheckedChanged event... whatever will be easier.
  • Fredrik Mörk
    Fredrik Mörk over 14 years
    @Grzenio: enabling/disabling controls in the Click even of a CheckBox is not a good idea; what if the Checked property is assigned by code? That does not trigger the Click event, and the UI will be in an inconsistent state. You should probably let that code be executed as a result of the CheckedChanged event instead.
  • Fandango68
    Fandango68 about 7 years
    Could the OP explain why this was the answer chosen, when it does not actually answer the question, which is to fire off the onclick event?!
  • Fandango68
    Fandango68 about 7 years
    This should have been the answer
  • Jeff Cyr
    Jeff Cyr over 5 years
    I extrapolated that OP didn't know about the CheckedChanged event. While not answering his specific question, it solve his root intent of being notified when a checkbox is changed programmatically.
  • Jeff Cyr
    Jeff Cyr over 5 years
    This only calls the click event handler, it does not "generate" a click and does not change the checkbox value.