none
Disabling a single item in a ListCheckBox

    Question

  • Question?

    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.

    Friday, December 16, 2005 10:16 PM

Answers

  • I 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:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    namespace Sample2

    {

        public partial class CheckListBoxTest : Form

        {

            public CheckListBoxTest()

            {

                InitializeComponent();

            }

     

            private void CheckListBoxTest_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("name");

                dt.Columns.Add("status",typeof(bool));

                for (int j = 0; j < 20; j++)

                {

                    dt.Rows.Add("checkbox"+j.ToString(), j%2);

                }

     

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

            }

     

            void listBox1_DrawItem(object sender, DrawItemEventArgs e)

            {

                e.DrawBackground();

                Rectangle contentRect = e.Bounds;

                contentRect.X = 16;

                e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(),

                    e.Font,

                    new SolidBrush(e.ForeColor),

                    contentRect);

            }

        }

    }

     

    Wednesday, February 28, 2007 9:59 AM
  • I 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.

    -chris
    Monday, December 19, 2005 2:48 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

    Monday, December 19, 2005 3:29 AM

All replies

  • I 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.

    -chris
    Monday, December 19, 2005 2:48 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

    Monday, December 19, 2005 3:29 AM
  • I 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:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

     

    namespace Sample2

    {

        public partial class CheckListBoxTest : Form

        {

            public CheckListBoxTest()

            {

                InitializeComponent();

            }

     

            private void CheckListBoxTest_Load(object sender, EventArgs e)

            {

                DataTable dt = new DataTable();

                dt.Columns.Add("name");

                dt.Columns.Add("status",typeof(bool));

                for (int j = 0; j < 20; j++)

                {

                    dt.Rows.Add("checkbox"+j.ToString(), j%2);

                }

     

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

            }

     

            void listBox1_DrawItem(object sender, DrawItemEventArgs e)

            {

                e.DrawBackground();

                Rectangle contentRect = e.Bounds;

                contentRect.X = 16;

                e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(),

                    e.Font,

                    new SolidBrush(e.ForeColor),

                    contentRect);

            }

        }

    }

     

    Wednesday, February 28, 2007 9:59 AM