简体   繁体   中英

CheckedChanged not firing?

I have done a lot of reading on this and every question I found involves ASP.NET. I'm using Winforms. I have a checkbox (Called CheckboxPicture ) on my main form. I want to run a few commands when the state of this checkbox is changed by the user.

This should do it:

public void CheckboxPicture_CheckedChanged(Object sender, EventArgs e)
{
    MessageBox.Show("Check State Changed");
}

However checking and unchecking the checkbox dont work. ASP.NET says you need

Autopushback = true but I'm not useing ASP.NET so im not sure where that would go.

A google search for "winforms checkbox event" yields this as its first result:

MSDN: CheckBox.CheckedChanged Event

At some point, they mention:

To run the example code, paste it into a project that contains an instance of type CheckBox named CheckBox1. Then ensure that the event handler is associated with the CheckedChanged event.

(Emphasis mine.)

Unfortunately, they don't show how to "ensure that the event handler is associated with the CheckedChanged event".

In short, somewhere within your code you have to have the following statement:

CheckboxPicture.CheckedChanged += CheckboxPicture_CheckedChanged

In other words, your CheckboxPicture_CheckedChanged() method will not be called by magic, you have to make sure it gets called when the corresponding event of the checkbox is fired.

Go to the form in your designer. Click on the checkbox and look at the properties box. Click on the event handlers and select your handler for the CheckedChanged handler property.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM