gpt4 book ai didi

c# - 如何将 int 或 bool 转换为 checkState

转载 作者:太空狗 更新时间:2023-10-29 17:35:25 25 4
gpt4 key购买 nike

我需要将 int 和/或 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());
}

最佳答案

根据评论更新:

要么将 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);

更新
仅供引用,没有编写 ValueCheck 方法,而是有一个 C# 运算符 ?: operator我在上面提到过,您可以这样做:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com