JavaScript Error

You currently have JavaScript disabled on your web browser.

This website uses JavaScript, and This web page needs JavaScript activated to work correctly.

Please active JavaScript on your web browser and then refresh this web page.



CheckedListBox



example0

The CheckedListBox control is found in the System.Windows.Forms namespace within the System.Windows.Forms.dll. In this blog post I am referring to the CheckedListBox control that is available in C# .NET-Core (.NET 6.0) with Visual Studio 2022 (although this code example might still work OK with older .NET versions).

The purpose and function of the CheckedListBox is to display a ListBox in which a check box is displayed to the left of each item listed. It servers to contain a group of items, where the user is able to check or uncheck each item individually, and acts similar to a collection of multiple CheckBox objects contained and displayed in a ListBox interface.


Example Source Code

This example uses a CheckedListBox component and two Label components.

To add the CheckedListBox control to your form, you can double click on its name (i.e. CheckedListBox) as listed in the Toolbox window panel within the Form editor window. Alternatively, you can single click on it and then drag and drop it onto your form, to position it more closer to where you want it to be positioned at. Once it is added to the form then it will appear on the forms surface area having default CheckedListBox control values.

After you have added the CheckedListBox control to your form, then once you select it then you can view and edit that objects property values in the Properties window within the Forms editor window, where you can then change the controls Name, Text, and other properties as you desire.

In the following example, using the Form editor I added a CheckedListBox control objects to my Form. I then selected the object and adjusted its Items property with the Properties Window and added several items to its items collection. I also added two labels to the form, and then adjusted the text value of one label to display "Check All That Apply:" and then renamed the other label "feedbackLabel":

example1

From the Form editor, I double clicked on the CheckedListBox object that I had just added to the Form. Doing so automatically creates and links a callback method to that controls SelectedIndexChanged event into that forms .cs source code file, which I can then program some action to be performed whenever an items checked state is changed.

In the following example, when the program is running and someone checks one of the check boxes then the feedbackLabel is updated to display how many check boxes are checked:

 
namespace TestProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // cound the number of items checked
            int totalItems = 0;
            for(int n=0; n < checkedListBox1.Items.Count; n++)
            {
                if(checkedListBox1.GetItemChecked(n) == true)
                {
                    totalItems++;
                }
            }

            // update the feedback label
            feedbackLabel.Text = Convert.ToString(totalItems) + " Items Checked";
        }
    }
}

Alternatively still, you can also instantiate a new instance of the CheckedListBox class object as a variable and then add it to the forms control collection manually within the programs source code. If you create and add the object manually within the programs source code instead of by drag and dropping the object in the Form editors window, then you will need to also set all of its properties and callback events manually as well, including its list items.

Below is an example source code of doing just that. For the sake of simplicity, I left the two labels created in the previous example on the form and just removed the CheckedListBox object, so that in the example below I can demonstrate how to programmatically create and add a new CheckedListBox object and one of its callback events from the source code instead of from the Form editor:

 
namespace TestProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // instantiate an instance of a new CheckedListBox object
            CheckedListBox checkedListBox1 = new CheckedListBox();

            // set the new CheckedListBox objects property values
            checkedListBox1.FormattingEnabled = true;
            checkedListBox1.Location = new System.Drawing.Point(12, 30);
            checkedListBox1.Name = "checkedListBox1";
            checkedListBox1.Size = new System.Drawing.Size(219, 76);
            checkedListBox1.TabIndex = 0;

            // add a range of new items to the new CheckedListBox objects items collection
            checkedListBox1.Items.AddRange(new object[] {
            "Item 1",
            "Item 2",
            "Item 3",
            "Item 4",
            "Item 5",
            "Item 6",
            "Item 7",
            "Item 8",
            "Item 9"});

            // link a callback method to the SelectedIndexChanged event
            checkedListBox1.SelectedIndexChanged += new System.EventHandler(this.checkedListBox1_SelectedIndexChanged);

            // add the new CheckedListBox object to this forms control collection
            this.Controls.Add(checkedListBox1);

        }

        private void checkedListBox1_SelectedIndexChanged(object? sender, EventArgs e)
        {
            // precheck
            if(sender == null) { return; }
            
            // get the object
            CheckedListBox checkedListBox1 = (CheckedListBox)sender;

            // count the number of items checked
            int totalItems = 0;
            for(int n=0; n < checkedListBox1.Items.Count; n++)
            {
                if(checkedListBox1.GetItemChecked(n) == true)
                {
                    totalItems++;
                }
            }

            // update the feedback label
            feedbackLabel.Text = Convert.ToString(totalItems) + " Items Checked";
        }

    }
}

When you run either of the above examples and click on their CheckBoxed Items then you should see something similar to the following:

example2

Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.


(0) pollYesResult
(0) pollNoResult



 
     About   |   Contact Us   |   Privacy   |   Terms & Conditions   |   © 2024 - T&J Divisions, LLC, All Rights Reserved