C# 复选框/切换按钮检查/取消选中本身
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10491029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checkbox/toggle button check/uncheck itself
提问by user1304490
I have a number of checkboxes that I want to handle them as toggle buttons. Each one is totally independent from the other and can be either checked or unchecked. When the user clicks on the checkbox/button it stays checked and if the user clicks again, then the checkbox/button returns to the "unchecked" state. Can the checkbox/button check/uncheck itself?
我有许多复选框,我想将它们作为切换按钮处理。每一个都完全独立于另一个,可以选中或不选中。当用户单击复选框/按钮时,它会保持选中状态,如果用户再次单击,复选框/按钮将返回“未选中”状态。复选框/按钮可以自己勾选/取消勾选吗?
I have been trying for a long time, searching also in the internet and cannot find a solution.
我已经尝试了很长时间,也在互联网上搜索,但找不到解决方案。
回答by Nikhil Agrawal
You can assign a common event to all Checkboxes. Write this code in your .cs file and select all Checkboxes and assign this event to checkedChange event.
您可以为所有复选框分配一个公共事件。将此代码写入您的 .cs 文件并选择所有复选框并将此事件分配给 checkedChange 事件。
private void chkboxes_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
if(!chk.Checked)
chk.Checked = true;
else
chk.Checked = false;
}
回答by Lyuben Todorov
Check the status of the check box, every time the button is clicked, change it.
检查复选框的状态,每次单击按钮时,更改它。
private void toggButton_CheckedChanged(object sender, EventArgs e)
{
// everytime you click the button checkbox states will change.
if(checkBox.Checked)
{
checkBox.Checked = false;
}
else
{
checkBox.Checked = true;
}
}
回答by Niranjan Singh
Ref: CheckBox.CheckStateproperty.
it is read and write property so you set it in GUI or through code.
参考:CheckBox.CheckState属性。
它是读写属性,因此您可以在 GUI 中或通过代码设置它。
CheckedThe CheckBox displays a check mark. The control appears sunken.
UncheckedThe CheckBox is empty. The control appears raised.
IndeterminateThe CheckBox displays a check mark and is shaded. The control appears flat.
CheckedCheckBox 显示一个复选标记。控件看起来凹陷。
未选中CheckBox 为空。控件出现凸起。
不确定CheckBox 显示一个复选标记并带有阴影。控件显得平坦。
write this on checked change event and check it is toggle or not.. it will work as like a switch..
在检查的更改事件上写这个并检查它是否切换.. 它会像一个开关一样工作..
label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" + "Checked: " +
checkBox1.Checked.ToString() + "\n" + "CheckState: " + checkBox1.CheckState.ToString();
for your question's answer check CheckBox.AutoCheck Property
对于您的问题的答案,请检查CheckBox.AutoCheck 属性
回答by ABCD
The problem is that the checkbox/button is "displayed" as always pressed i.e. the "blue" color that it has when checked is not changed giving the impression that it is not unchecked. – user1304490
问题是复选框/按钮总是被“显示”,即它在选中时的“蓝色”颜色没有改变,给人的印象是它没有被取消选中。– 用户1304490
May be setting CheckBox.ThreeStateproperty false helps.
可能设置CheckBox.ThreeState属性 false 有帮助。
If the ThreeState property is set to false, the CheckState property value can only be set to the Indeterminate value of System.Windows.Forms.CheckState in code and not by user interaction.
如果 ThreeState 属性设置为 false,则 CheckState 属性值只能在代码中设置为 System.Windows.Forms.CheckState 的 Indeterminate 值,而不能通过用户交互设置。
回答by ttvd94
A very short command:
一个非常简短的命令:
checkBox.Checked = !checkBox.Checked;
回答by Ashraf Abusada
When you want to change check box state for too many check boxes on one form, you need fast reliable code, without complexity.
当您想更改一个表单上太多复选框的复选框状态时,您需要快速可靠的代码,而不复杂。
The best way for doing it is using:
最好的方法是使用:
CheckBox.CheckState
CheckBox.CheckState
you can change Check Boxes state to (unchecked) for example using this code:
例如,您可以使用以下代码将复选框状态更改为(未选中):
checkbox1.CheckState = CheckState.Unchecked;
checkbox2.CheckState = CheckState.Unchecked;
checkbox3.CheckState = CheckState.Unchecked;
For further reading, see thisfrom MSDN.
如要进一步了解,请参阅该从MSDN。

