开发者

How to make a CheckBox unselectable?

I was wondering how you make a CheckBox unselectable in c#? I thought it would be something like SetSelectable (false) or something but I 开发者_JS百科can't seem to see the method.

I found CanSelect but this seems to be a read only property.


You can set AutoCheck property to false.


You can set the Enabled property to false:

checkBox1.Enabled = false;


You can create one by using following code

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

or also you can handle the checked change event and always set it back to value you want


In order to make a more read-only behavior:

  • Disable highlight when the cursor is over the CheckBox
  • Disable reacting (logically or visibly) to a mouse click
  • Have tooltips enabled

We can inherit the CheckBox class (similar to Haris Hasan's answer but with some improvements):

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


You can set the Enabled property to false


AutoCheck doesn't exist in UWP but I think you can use IsTapEnabled = false.


In response to some comments about grayed out label: Set the Text of the CheckBox to the empty string, Enableto false and use a Label for the text.

Drawback: No mnemonic support out of the box.


Leverage a simple Label using a glyph like this for its Text: ✔ checkBox1.


This code worked for me:

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


For disabling all the checkboxes in a 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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜