?
Solved

Cancel CheckedChanged event?

Posted on 2004-09-07
6
Medium Priority
?
2,500 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
[X]
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
6 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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