我想知道你如何在c#中使一个复选框无法选择?我认为它会像setSelectable(false)之类的东西,但我似乎无法看到这个方法.
我发现canSelect但这似乎是一个只读属性.
谢谢
ano*_*ery 17
您可以将Enabled属性设置为false.IE浏览器.checkBox1.Enabled = false;
编辑:太慢:P
您可以使用以下代码创建一个
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
或者您也可以处理已检查的更改事件,并始终将其设置回您想要的值
为了使更多的只读行为:
CheckBox我们可以继承CheckBox该类(类似于Haris Hasan 的回答,但有一些改进):
public class ReadOnlyCheckBox : CheckBox
{
[System.ComponentModel.Category("Behavior")]
[System.ComponentModel.DefaultValue(false)]
public bool ReadOnly { get; set; } = false;
protected override void OnMouseEnter(EventArgs e)
{
// Disable highlight when the cursor is over the CheckBox
if (!ReadOnly) base.OnMouseEnter(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
// Disable reacting (logically or visibly) to a mouse click
if (!ReadOnly) base.OnMouseDown(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
// Suppress space key to disable checking/unchecking
if (!ReadOnly || e.KeyData != Keys.Space) base.OnKeyDown(e);
}
}
Run Code Online (Sandbox Code Playgroud)