tags:

views:

101

answers:

2

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());
}
+4  A: 
  • Consider CheckBox.Checked which is the boolean property.
  • Use box.CheckState = (CheckState)ValueCheck;
  • You can also use the ?: 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;
Shimmy
I cant use CheckBox.Checke
Power-Mosfet
Why can't you, please explain and we'll try to look for a better option.
Shimmy
I have edited my question, please take a look.
Power-Mosfet
thank but this is not answer to my question. I need a 0 or 1 return. an integer.
Power-Mosfet
Simply cast it back to int, see my updated answer. In the update the ValueCheck variable contains your integer result and if you don't use interminate (CheckBox.ThreeState property) it will return either 0 or 1, **an integer**.
Shimmy
A: 

It appears ValueCheck should be either 1 or 0 representing true and false respectively, in which case you should use this:

this.gs_check2.Checked = ValueCheck == 1;

EDIT: Based on your edit it seems what you want is this:

CheckState state = (CheckState)this.ValueCheck;
box.CheckState = state;
this.gsCheck2.CheckState = state;

However note that it could be possible that ValueCheck could contain an invalid value for the CheckState enumeration.

Lee
this.gs_check2.Checked = ValueCheck; not works
Power-Mosfet
He said ==, not =.
Shimmy
ValueCheck has already a positive or negative value so why == 1;
Power-Mosfet
@Power-Mosfet - What are the values you expect for `ValueCheck`? The comment suggests it should be 0 or 1. You need to update your question to explain how you want the CheckState of the checkbox to be mapped to different possible values of ValueCheck.
Lee
@Power-Mosfet - The box.CheckedState returns an Enum value, which you have to cast to int. besides it may also contain the value 2.
Shimmy