I need to convert int and or bool to checkState
int ValueCheck;
private void gsCheck1_CheckedChanged(object sender, EventArgs e)
{
CheckBox box = sender as CheckBox;
box.CheckState = ValueCheck; // doesn't work
this.gsCheck2.CheckState = ValueCheck; // should be 1 or 0 ?
}
As you can see I want to change (this.gsCheck2) CheckState by changeing (this.gsCheck1) CheckState and end up with a integer value which is need.
Update.... problem solved
private int ValueCheck(CheckState Check)
{
if (Check == CheckState.Checked)
return 1;
else
return 0;
}
private void gs_CheckedChanged(object sender, EventArgs e)
{
CheckBox box = sender as CheckBox;
MessageBox.Show(box.Name + "="+ ValueCheck(box.CheckState).ToString());
}
CheckBox.Checked
which is the boolean property.box.CheckState = (CheckState)ValueCheck;
?:
operator.Update according to comments:
Either declare the ValueCheck as a CheckState:
CheckState ValueCheck;
private void....
Or convert the int value to a CheckState value:
this.gsCheck2.CheckState = (CheckState)ValueCheck;
The cast back the CheckState value to int:
CheckState cs = box.CheckState;
int ValueCheck = (int)cs;
string result = "Current state: " + ValueCheck + cs.ToString();
//You question:
MessageBox.Show(box.Name + (int)box.CheckState);
Update
FYI, instead of writing the ValueCheck method, there is a C# operator ?:
operator I mentioned above, which you can do:
int result = box.CheckState == CheckState.Checked ? 1 : 0;
Which is a translation of:
int result;
if (box.CheckState == CheckState.Checked)
result = 1;
else
result = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With