Cancel CheckedChanged event?

Posted on 2004-09-07
Last Modified: 2012-06-27
I have an interesting problem that I'm not sure has an elegant solution.

I have a Win form with a bunch of controls. One of them is a checkbox. When the user checks or unchecks the checkbox, the CheckedChanged event is fired. That event has some code which does certain things.

What I'd like is when the user clicks the checkbox to check or uncheck it, there is a condition I want to check first and based on that, it'll either check or uncheck the control.

Right now, when the control is checked, the event is fired and the checked property is changed and then it runs the code to check the condition and based on that, I might have to programmatically reverse back to the previous checked property. And when I reverse it, the CheckedChanged property is called again (and again and again). The workaround for this is ugly.

Is there an elegant solution for this?
Question by:hobster
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 20

Accepted Solution

TheAvenger earned 50 total points
ID: 12003961
When the CheckedChanged event is fired, the checked status is already changed. You have two options:

1. Have a boolean member like
private bool currentlyManualChangingState = false;
During the verifications, if you need to change the state, set this member to true and make the change. In the beginning of your verification code, check the value of the member and if it is true (which means you are currently setting the value manually) do nothing. Don't forget to return the value to false when finished. It's something like:

if (!currentlyManualChangingState){
  currentlyManualChangingState = true;
  // Verifications
  // Reset checked state if needed
  currentlyManualChangingState = false;

2. You can use the mouse clicked, key pressed and other events to find out when exactly the check box should be checked/unchecked. In this case you can make the verification and then send the event to the default handler only if the conditions are met. Thus the checked state will not changed if the conditions are not met. The problem is that there are a lot of events that can change the value of the check box and you have to handle all of them.

Author Comment

ID: 12007264
With the 1st option, how can I prevent the CheckedChanged event from being fired? Where would I put the verification code?
LVL 20

Expert Comment

ID: 12007912
You would inherit from CheckBox and override some methods, like OnMouseDown for example. There you will check if the mouse was clicked over the check box itself (not over the text part of it) and call base.OnMouseDown only if you want the checked state to be changed. Otherwise you would not call the base, so the mouse would "not" click the check box and the checked property will not be changed. Same with keys to get the space, which changes the checked state
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 12008647
The last post sounds similar to the 2nd option. Is it? What option is easier and do you have sample code for it?

I'll increase points for sample code. Thanks.
LVL 20

Expert Comment

ID: 12010423
Oh, sorry, the last post was really for the 2nd option. For the 1st one you cannot stop the CheckedChanged to be fired. The code I gave you goes into the CheckedChanged event handler, just when it changes the checked state, it is fired again, but does nothing

Author Comment

ID: 12022990

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Securing WEBAPI on Azure 2 54
c# ftp code 3 53
Example code 13 37
Trying to check to see if email is in valid format in 3 15
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question