如何使复选框无法选择?
我想知道你如何在c#中使一个复选框无法选择? 我认为它会像setSelectable(false)之类的东西,但我似乎无法看到这个方法。
我发现canSelect但这似乎是一个只读属性。
谢谢
您可以将Enabled
属性设置为false
。 IE浏览器。 checkBox1.Enabled = false;
编辑:太慢:P
您可以将AutoCheck属性设置为false
。
您可以使用以下代码创建一个
public class ReadOnlyCheckBox : System.Windows.Forms.CheckBox { private bool readOnly; protected override void OnClick(EventArgs e) { // pass the event up only if its not readlonly if (!ReadOnly) base.OnClick(e); } public bool ReadOnly { get { return readOnly; } set { readOnly = value; } } }
或者您也可以处理已检查的更改事件,并始终将其设置回您想要的值
如何将Enabled
属性设置为false
?
禁用checkedlistbox中的所有复选框
for (int i = 0; i < checkedListBoxChecks.Items.Count; i++) { checkedListBoxChecks.SetItemChecked(i, true); //checkedListBoxChecks.Enabled = false; this.checkedListBoxChecks.SetItemCheckState(i, CheckState.Indeterminate); } private void checkedListBoxChecks_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.CurrentValue == CheckState.Indeterminate) { e.NewValue = e.CurrentValue; } }
UWP中不存在AutoCheck,但我认为您可以使用IsTapEnabled = false。
回应一些关于灰色标签的注释:将CheckBox
的Text
设置为空字符串, Enable
为false
并使用Label
作为文本。
缺点:没有开箱即用的助记符支持。
使用像这样的字形的简单Label
用于其Text
: ✔ checkBox1
。
这段代码对我有用:
public class CtrlCheckBoxReadOnly : System.Windows.Forms.CheckBox { [Category("Appearance")] [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] public bool ReadOnly { get; set; } protected override void OnClick(EventArgs e) { if (!ReadOnly) base.OnClick(e); } }
即使单击了您在复选框单击事件中写入(checkboxname.checked = true) ,也可以使用最简单的方法来复选复选框。 每次用户点击它时,都会显示已选中状态。