i want to disable few check boxes in a checkedlistbox. what should i do?

Dani AI

Generated

It helps to clarify platform first. Are you on WinForms CheckedListBox or ASP.NET CheckBoxList? is manually overlaying CheckBox controls on a ListBox, which is more work than needed. With a standard CheckedListBox, you cannot truly disable an individual item, but you can block it from changing state. Also, as suggested, you do not need a nested loop; a single range loop is enough.

WinForms approach (block toggling via ItemCheck). Indices are 0-based: the 100th and 101st items are indices 99 and 100; items 48..112 are indices 47..111.

// declare once
HashSet<int> disabled = new HashSet<int> { 99, 100 }; // 100th and 101st items

// or: disable items 48..112 (when you have 112 total)
for (int i = 47; i < checkedListBox1.Items.Count; i++)
    disabled.Add(i);

// wire once, e.g., in Form_Load
checkedListBox1.ItemCheck += (s, e) =>
{
    if (disabled.Contains(e.Index))
        e.NewValue = e.CurrentValue; // veto the change
};

ASP.NET WebForms CheckBoxList supports per-item disabling directly. Again, indices are 0-based:

// disable 100th and 101st items
CheckBoxList1.Items[99].Enabled = false;
CheckBoxList1.Items[100].Enabled = false;

// disable items 48..112 (inclusive) when there are 112 total
for (int i = 47; i < CheckBoxList1.Items.Count; i++)
    CheckBoxList1.Items[i].Enabled = false;

UX tip: if you block items in WinForms, consider visually signaling it (e.g., owner-draw to gray the text) so users understand they are read-only. In ASP.NET, remember disabled inputs are not posted back; always validate on the server that restricted items remain unchanged.

Recommended Answers

All 4 Replies

private void CheckBoxListTest_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            for (int j = 0; j < 20; j++)
            {
                dt.Rows.Add("checkbox" + j.ToString());
            }

            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 = true;
              cks[j].Enabled = false;
                cks[j].Height = this.listBox1.ItemHeight;
                cks[j].Width = 15;
                cks[j].Location = new Point(0, this.listBox1.ItemHeight * j);
                this.listBox1.Controls.Add(cks[j]);
                this.listBox1.Items.Add(dt.Rows[j]["name"].ToString());
            }
            this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
            this.listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
        }

if my checklistbox contains 112 items. what should i do to disable item 100 and 101, without increasing number of items?

i have 112 items in checklist box.i want to disable items from 48 to 112.please help me

As pritishdeshmuk mentioned ,

just add the condition

if (j <= 10)
                     {
                         for (int j1 = j; j1 < 20; j1++)
                         {
                             cks[j].Enabled = false;
                         }
                     }

Hope this will help you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.