Click to See Complete Forum and Search --> : CheckBox Control Use
thoalex
March 21st, 2005, 11:57 PM
Ok, I'm new to MS programming so I'm sure that everyone on the planet knows the answer to this.
I have 4 checkboxes on a form. When one of the checkboxes are "checked", I want to clear the checkboxes (uncheck) them from the other 3 (one of them is checked but I don't know which). However, when I set the state of the other 3 checkboxes this fires off the CheckedStateChanged event handler.
How does someone do this???? Remove the call back function from the event handler before changing the state? Is that the only way to do it?
Thanks
Thoalex
Issa
March 22nd, 2005, 01:54 AM
you could do something like this
//within your form declarations
System.Windows.Forms.CheckBox[] checkBoxes = null;
...
// subscribed to the form's load event
private void Form1_Load(object sender, System.EventArgs e)
{
// load all your CheckBoxes of interest into a an array for later reference
this.checkBoxes = new CheckBox[]{ this.checkBox1, this.checkBox2, this.checkBox3 };
}
// subscribe this method to the CheckedChanged event for all CheckBoxes of interest
private void checkBoxes_CheckedChanged(object sender, System.EventArgs e)
{
foreach ( CheckBox checkBox in this.checkBoxes )
{
if ( !checkBox.Focused ) { checkBox.Checked = false; }
}
}
this is very clean semantically but for each check of a CheckBox past the first, it results in a finite cascade of checks and invocations.
another option is as you mention above, start subscription to events dynamically.
this time, we will not begin by subscribing any method to a CheckChanged event.
install, we will handle this by monitoring the focus:
// declare as field in your form
private CheckBox[] checkBoxes = null;
...
private void Form1_Load(object sender, System.EventArgs e)
{
this.checkBoxes = new CheckBox[]{ this.checkBox1, this.checkBox2, this.checkBox3 };
foreach ( CheckBox checkBox in this.checkBoxes )
{
checkBox.Enter += new EventHandler(this.checkBoxes_Enter);
checkBox.Leave += new EventHandler(this.checkBoxes_Leave);
}
}
private void checkBoxes_CheckedChanged(object sender, System.EventArgs e)
{
foreach ( CheckBox checkBox in this.checkBoxes )
{
if ( !checkBox.Focused ) { checkBox.Checked = false; }
}
}
private void checkBoxes_Enter( object sender, System.EventArgs e )
{
CheckBox checkBox = sender as CheckBox;
if ( checkBox != null ) { checkBox.CheckedChanged += new System.EventHandler( this.checkBoxes_CheckedChanged ); }
}
private void checkBoxes_Leave( object sender, System.EventArgs e )
{
CheckBox checkBox = sender as CheckBox;
if ( checkBox != null ) { checkBox.CheckedChanged -= new System.EventHandler( this.checkBoxes_CheckedChanged ); }
}
but this is too complicated and probably results in just as much work as the first example.
although i prefer the semantics of the first example, probably the cleanest is to just remember which CheckBox was last checked, and then uncheck it.
// declare as a field in your form
private CheckBox lastCheckedBox = null;
...
// subscribe this method to the CheckedChanged event of all interested CheckBoxes
private void checkBoxes_CheckedChanged( object sender, System.EventArgs e )
{
this.resetCheckBoxes( sender as CheckBox );
}
private void resetCheckBoxes( CheckBox checkBox )
{
if ( checkBox == null ) { return; }
if ( checkBox.Checked == false ) { return; }
if ( this.lastCheckedBox == null )
{
this.lastCheckedBox = checkBox;
return;
}
if ( this.lastCheckedBox != checkBox )
{
CheckBox tempLast = this.lastCheckedBox;
this.lastCheckedBox = checkBox;
tempLast.Checked = false;
}
}
hspc
March 22nd, 2005, 02:15 AM
When one of the checkboxes are "checked", I want to clear the checkboxes (uncheck) them from the other 3 (one of them is checked but I don't know which). However, when I set the state of the other 3 checkboxes this fires off the CheckedStateChanged event handler.
Why don't just use OptionButton Controls?
Norfy
March 22nd, 2005, 06:26 AM
Why don't just use OptionButton Controls? Otherwise known as a RadioButton in C#.
If for some reason you still want CheckBoxes instead, another possibility...
bool clearingCheckBoxes = false;
private void checkBoxes_CheckedChanged(object sender, System.EventArgs e)
{
if (clearingCheckBoxes) return;
this.clearingCheckBoxes = true;
foreach (CheckBox checkBox in this.checkBoxes)
{
if (sender != checkBox)
{
checkBox.Checked = false;
}
}
this.clearingCheckBoxes = false;
}
thoalex
March 22nd, 2005, 02:30 PM
Sorry, I'm a 15 year developer but I'm new to doing much on the MS platform.
I didn't know about RadioButton which I'll look into.
And, since I am new to this, I'm overthinking sometimes. I did pretty much the same solution as Norfy. I just thought there might be some easy way to turn off the event handling.
Thanks for the help!
Thoalex
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.