Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a checkbox in a checkedlistbox?

Tags:

I have some items in a CheckedListBox, I want to disable the CheckBox of first item in it.
i.e. I want to disable the first item in the CheckedListBox, because I want to tell the user visually that option is not available.

like image 313
Subodh Bansal Avatar asked Dec 06 '10 16:12

Subodh Bansal


People also ask

How to disable checkbox in checkboxlist in c#?

chkBoxlist. DataValueField = "state"; Then you can use ValueField to disable your checkboxlist item in terms of the state value in your Page_Load event by if else cause.

How do you make a checkbox Unselectable?

We can make a checkbox disabled in HTML using the disabled attribute. We assign the disabled attribute to an input to disable that input. We can also use the jQuery prop() method to change the disabled attribute of an input to true.


1 Answers

Combining 2 of the above partial answers worked great for me. Add your items to the list with:

myCheckedListBox.Items.Add(myItem, myState);

Where myState is CheckState.Indeterminate for items that should be disabled. Then add an event handler to keep those items from being changed:

myCheckedListBox.ItemCheck += (s, e) => { if (e.CurrentValue == CheckState.Indeterminate) e.NewValue = CheckState.Indeterminate; };

This does not allow you to use 'Indeterminate' in this list for its normal purpose but it does give a look very similar to what one would expect for a disabled item and it provides the correct behavior!

like image 134
Mick Bruno Avatar answered Sep 30 '22 13:09

Mick Bruno