[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cancel CheckedChanged event?

Posted on 2004-09-07
6
Medium Priority
?
2,545 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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…

649 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