Friday, December 16, 2005 10:16 PM
Is it possible to disable a single item in a ListCheckBox, and if so, how can it be done?
I have a ListCheckBox that lists several items. In some cases items are prechecked. I would want those items to be disabled so that the user cannot uncheck them.
Also, the number of items the user can select are limited, so when they make choices, other items may need to be disabled so that they cannot check them.
Monday, December 19, 2005 2:48 AMI don't know of any method that would allow you to disable any item from a CheckedListBox, and I don't think it is possible either in a CheckedListBox that comes from the box.
What you can do however is to temporarily remove the item from the CheckedListBox so the user won't be able to check/uncheck it, or use a third-party CheckedListBox control that has this feature.
Monday, December 19, 2005 3:29 AM
You could probably use a ListView with CheckBoxes set to true, instead of the CheckedListBox. Then you have the ItemChecked event which is called after an item's checkbox is clicked. You can use that event to re-check an item that can't be unchecked, or to validate check count or whatever you want to do.
I hope this helps!
Luis Alonso Ramos
Wednesday, February 28, 2007 9:59 AMI know this thread is old, but my sample will help the person who find this thread by searching. In fact, the CheckListBox is inherited from ListBox, i.e, CheckListBox is kind of a ListBox, so, we can make a ListBox to work as a CheckListBox, here is my sample for you:
public partial class CheckListBoxTest : Form
private void CheckListBoxTest_Load(object sender, EventArgs e)
DataTable dt = new DataTable();
for (int j = 0; j < 20; j++)
CheckBox cks = new CheckBox[dt.Rows.Count];
for (int j = 0; j < dt.Rows.Count; j++)
cks[j] = new CheckBox();
cks[j].Text = "";
cks[j].Checked = (bool)dt.Rows[j]["status"];
cks[j].Enabled = !cks[j].Checked;
cks[j].Height = this.listBox1.ItemHeight;
cks[j].Width = 15;
cks[j].Location = new Point(0, this.listBox1.ItemHeight * j);
this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
Rectangle contentRect = e.Bounds;
contentRect.X = 16;