Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...