Solved

Cancel CheckedChanged event?

Posted on 2004-09-07
6
2,258 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Author Comment

by:hobster
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now