如何使复选框无法选择?

时间:2011-05-11 08:07:23

标签: c# winforms

我想知道如何在c#中创建一个无法选择的复选框?我认为它会像setSelectable(false)之类的东西,但我似乎无法看到这个方法。

我找到了canSelect,但这似乎是一个只读属性。

由于

11 个答案:

答案 0 :(得分:116)

您可以将AutoCheck属性设置为false

答案 1 :(得分:15)

您可以将Enabled属性设置为false。 IE浏览器。 checkBox1.Enabled = false;

编辑:太慢了:P

答案 2 :(得分:8)

您可以使用以下代码

创建一个
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; }
        }
}

或者您也可以处理已检查的更改事件并始终将其设置回您想要的值

答案 3 :(得分:1)

如何将Enabled属性设置为false

答案 4 :(得分:1)

为了进行更只读的操作:

  • 当光标位于CheckBox上方时禁用突出显示
  • 禁用(逻辑上或视觉上)对鼠标单击的反应
  • 启用了工具提示

我们可以继承类似于Haris Hasan's answer的CheckBox:

public class ReadOnlyCheckBox : System.Windows.Forms.CheckBox
{
    [System.ComponentModel.Category("Behavior")]
    [System.ComponentModel.DefaultValue(false)]
    public bool ReadOnly { get; set; } = false;

    protected override void OnMouseEnter(EventArgs eventargs)
    {
        // Disable highlight when the cursor is over the CheckBox
        if (!ReadOnly) base.OnMouseEnter(eventargs);
    }

    protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs mevent)
    {
        // Disable reacting (logically or visibly) to a mouse click
        if (!ReadOnly) base.OnMouseDown(mevent);
    }
}

答案 5 :(得分:0)

禁用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;
            }
        }

答案 6 :(得分:0)

UWP中不存在AutoCheck,但我认为您可以使用IsTapEnabled = false。

答案 7 :(得分:0)

回复一些关于变灰标签的评论:将Text的{​​{1}}设置为空字符串,将CheckBox设置为Enable并使用false为文本。

缺点:没有开箱即用的助记符支持。

答案 8 :(得分:0)

使用这样的字形为git rebaseLabel利用一个简单的Text

答案 9 :(得分:0)

此代码对我有用:

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);
    }
}

答案 10 :(得分:-3)

即使点击了您在复选框点击事件中写入(checkboxname.checked = true),也可以选中始终检查复选框的最简单方法。 每次用户点击它时,都会显示已检查。

相关问题