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 cant seem to see the method.

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

Thanks

Answers


You can set the Enabled property to false. Ie. checkBox1.Enabled = false;

EDIT: Too slow :P


You can set AutoCheck property to 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


How about setting the Enabled property to false?


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 similar to Haris Hasan's answer:

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

For disable all the checkboxes in 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;
            }
        }

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

The simplest way you can make a checkbox always checked even if clicked is that you write (checkboxname.checked=true) in the checkbox click event. and every time the user clicks on it, it states checked.


Need Your Help

How can I plot this data?

python numpy matplotlib plot

I am trying to plot this data:

How to edit content using Tiny Mce

jquery tinymce

I am using Tiny MCE to edit some content within my site. I am having problems displaying text within the Tiny MCE content area ,which has been retrieved from the database.