我需要将int和or bool转换为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 ?
}
如您所见,我想通过更改(this.gsCheck1)CheckState来更改(this.gsCheck2)CheckState,最后得到一个需要的整数值。
更新....
问题解决了
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
,它是 bool 属性。 box.CheckState = (CheckState)ValueCheck;
?:
operator。 根据评论更新:
可以将ValueCheck声明为CheckState:
CheckState ValueCheck;
private void....
或将int值转换为CheckState值:
this.gsCheck2.CheckState = (CheckState)ValueCheck;
将CheckState值强制转换为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);
更新
仅供引用,我上面提到了一个C#运算符
?:
operator,而不是编写ValueCheck方法,您可以执行以下操作:int result = box.CheckState == CheckState.Checked ? 1 : 0;
这是以下内容的翻译:
int result;
if (box.CheckState == CheckState.Checked)
result = 1;
else
result = 0;
关于c# - 如何将int或bool转换为checkState,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3221137/