Solved

Cancel CheckedChanged event?

Posted on 2004-09-07
6
2,285 Views
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?
0
Comment
Question by:hobster
  • 3
  • 3
6 Comments
 
LVL 20

Accepted Solution

by:
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.
0
 
LVL 2

Author Comment

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

Expert Comment

by:TheAvenger
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:hobster
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.
0
 
LVL 20

Expert Comment

by:TheAvenger
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
0
 
LVL 2

Author Comment

by:hobster
ID: 12022990
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different Delete Messages 7 37
This code tracks birthdays 3 56
DateTimepicker 4 33
Help with C#, MVC, razor. 6 18
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now