Hey, everyone, hope you are doing great. In today's article, we are going to explore C# Checked ListBox Control. In the previous article, we discussed in details about C# TextBox, C# Label, C# ListBox, C# ComboxBox and C# Button. C# Checked ListBox plays very important role in Point Of Sale applications. In some words, we can say this is a combination of check boxes and list. C# Checked ListBox is used to gain specific information from end-user. It's mostly used in survey-based application to gain the voting for a specific purpose. We will work with Checked ListBox same as we have worked with ListBox & ComboBox. You can use C# Checked ListBox for multiple purposes. You can use this as the attendance taker to mark the attendance of students.

C# Checked ListBox Control

The C# Checked ListBox is simple list box with the combination of checks. It provides you the list and checks to mark them as you select the items. The user can select the single and multiple checks. According to the need, you can set the checks enabled and disabled. You can create conditional statements on which C# Checked ListBox will become enabled and disabled. When professional programmers used Checked ListBox they will also use the Conditional Statements. They make sure that checked boxes will become enabled only when user will fill up or proceed the above instructions. A live example of checkbox usage is the terms & condition form of any organization. If you are getting started with YouTube they will be asked you to read their terms and checked in the last that you are agreed or not. If you will check the box only then you can process forward. Same is the case will C# Checked ListBox, developers make the conditional check for inputs gained from C# Checked ListBox. If you are wanted to add the Checked ListBox, then go to the toolbox in design tab. Drag the C# Checked ListBox on the form. Now the CheckListBox is added to the form. The default name of first drag Checked ListBox is as CheckedListBox1. You can change this name from the property tab on the right-hand side. Supposed you are wanted to add values in C# Checked ListBox, then you have to write items after the default name and select the add method. In the following code, you can be observed how we inserted the values inside C# Checked ListBox.
using System;
using System.Drawing;
using System.Windows.Forms;

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

        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }
    }
}
We have used the button. When user will click the button, C# Checked ListBox values appears. It depends on your logic that how you wanted to process the codes. I have used the button if you don't want to use the button you can simply copy the code after the InitializeComponent function. It will generate the following output when a button is clicked.
C# Checked ListBox

C# Checked ListBox Properties

If you are looking to change the default styles, color scheme, font size etc then there are many properties which you can use to modify the Checked ListBox. Default Checked ListBox is simple in appearance, you can make that attractive by customising the properties. In the following part of an article, we will discuss, how to change foreground color and background color of a checked list box. How to change the font size and make checked and uncheck marks on checked ListBox items. If you are wanted to make the Checked ListBox items marked check when executing the program, then you have to use the CheckState attribute. There are three values which you can use for CheckState attribute.
  • Checked
  • UnChecked
  • Indeterminate
In the following code, we have used all the above values for CheckState attribute. By default all the Checked ListBox items are UnChecked so you don't need to write the UnChecked values.
using System;
using System.Drawing;
using System.Windows.Forms;

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

        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Add("TEP 1", CheckState.Checked);
            checkedListBox1.Items.Add("TEP 2", CheckState.Indeterminate);
            checkedListBox1.Items.Add("TEP 3", CheckState.Unchecked);
            checkedListBox1.Items.Add("TEP 4");
        }
    }
}
We don't use the CheckState for the 4th item because by default it's unchecked. If you will execute the code, you observed that Check will be marked, Indeterminate is Check but can't edit and uncheck is unmarked. If you can't understand then the below image will the exact output of the above code.
C# Checked ListBox
If you are looking to change the background color then you have to use the backColor property. BackColor property is applicable on all tools which are available in ToolBox. In the following code, we are going to change the background color of Checked ListBox from white to AliceBlue. We will use the Color property to set the color. You can get the idea from the code.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.BackColor = Color.AliceBlue;
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
    }
}
If you are wanted to change the foreground color or color of the font, then you have to use the ForeColor property to set the colors. We will be used the Color property to set the value of color which is assigned to the foreColor. In the following code, we have used BlueViolet color and set it for the foreground or for the font.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.ForeColor = Color.BlueViolet;
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
    }
}
If you are wanted to change the font size, then you have to used to set the font property. We have to call the font constructor and passed two parameters. The first parameter is Font.FontFamily and the second parameter is the size of the font. In the following code, you can observe that how we changed the font size for Checked ListBox. The first parameter is the prototype and the second parameter is the font style property.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Font = new Font(Font.FontFamily, 12);
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
    }
}
If you are wanted to change the text style into italic, then you have to use the FontStyle property. We have to use the font constructor and passed two parameters. The first parameter is the prototype and the second one is the FontStyle.Value which is set to Italic in the following code. We have passed the CheckedListBox1.Font as the prototype parameter.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Font = new Font(checkedListBox1.Font, FontStyle.Italic);
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        } 
    }
}
If you are wanted to bold the text, then you have to use the font constructor and passed the Font style as the bold. In the following code, we have changed the text of checkedListBox items to bold.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Font = new Font(checkedListBox1.Font, FontStyle.Bold);
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
    }
}
If you are wanted to change the font family style then you have to use the font constructor and passed the font family name and size as the parameter. The first parameter is the name of font family and the second parameter is the font size. In the following code, we have used the "Times New Roman" and the size as 20.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Font = new Font("Times New Roman",20);
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
    }
}
If you are wanted to check that what the value you have selected, then you have to use the SelectedItem. In the following code, I have used the button and message box to show the selected item value. If you will execute the code, then you have to select the items first then click the button and it returns the selected item value in the message box.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(checkedListBox1.SelectedItem.ToString());
        }

       
    }
}
If you are wanted to verify that, which items are marked or check, then you have to use the foreach loop. In the following code, we have used the foreach loop to get the value from checkedListBox1.CheckedItems into an object-based variable which is itemCheck. Then we have to use message box to show the value of itemCheck in each step of a loop by converting the object type value into a string by calling the ToSting method.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

        private void button1_Click(object sender, EventArgs e)
        {          
         foreach (object itemChecked in checkedListBox1.CheckedItems)
            {           
                MessageBox.Show(itemChecked.ToString());
            }
        }   
    }
}
If you are wanted to change the border style of Checked ListBox, then you have to use the BorderStyle property and set the style according to your need. There are three styles which you can apply, all those styles are mention in the following code. You can check them one by one, if you will execute all of these styles at once then only the last one is getting executed and other above will get ignored by the compiler.
checkedListBox1.BorderStyle = BorderStyle.Fixed3D;
checkedListBox1.BorderStyle = BorderStyle.FixedSingle;
checkedListBox1.BorderStyle = BorderStyle.None;

C# Checked ListBox Events

C# provides various events to utilize with GUI tools. We can be used events to Checked ListBox and enhanced its features. Events are introduced to create conditional checks for the users to perform specific functionalities at specific times. You can change any attribute when user will click on the listed or checked items. Changed the color when items mark checked. It depends on the requirement of the software in which you will use the CheckedListBox. Following are some events which we are going to demonstrate with Checked ListBox.
  • Checked ListBox Click Event
  • Checked ListBox MouseHover Event
  • Checked ListBox MouseLeave Event
  • Checked ListBox BackColorChanged Event
  • Checked ListBox ForeColorChanged Event
  • Checked ListBox FontChanged Event
There are many other events, which we can use. Each event has its own logic of occurrence. Some events are common across the GUI Tools and some events are specifically used by GUI Tools. Checked ListBox Click Event This event will be executed when the user clicks within the boundaries of Checked ListBox. Whatever user will click on any listed item, checkbox or border it will get executed. To get the concept of the click event, suppose that you are wanted when someone clicks on CheckedListBox then the message will pop up and show notification of click. In the following program, we have created the code, which can execute in your compiler too.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

       
        private void checkedListBox1_Click(object sender, EventArgs e)
 {
 MessageBox.Show("You have Clicked !!");
 }  
    }
}
An output of the above code is given in the following image. You can perform any functionality as you required. For the demonstration purpose just used the message box to reflect any action.
C# Checked ListBox
In the above code, we have to use the click event, if you want to activate the click event then simply click on the CheckedListBox in the design tab and it will create automatically click function. There is another option to do this, first, create any simple function and then set that function to the click event. In the following image, you can observe that when you click on CheckedListBox _Click function is set to click event.
C# Checked ListBox
  Checked ListBox MouseHover Event This event will be executed when mouse will hover the boundaries of Checked ListBox. To clear the concept in the following code we are going to perform mousehover function. When user will hover the mouse on the boundaries event will get executed and show the message popup.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }

        private void checkedListBox1_MouseHover(object sender, EventArgs e)
        {
            MessageBox.Show("Mouse is Hover !!");
        }   
    }
}
The above function is builtin and if you want to set the user define function then you can set by your own in the event property. You can perform any kind of functionality, we have used the message box, if you wanted to change anything then you can change such as the color, font family, and size of the text. In the following image, you can view the output of the above code.
C# Checked ListBox
Checked ListBox MouseLeave Event This event will be executed when the cursor of the mouse will leave the boundaries of Checked ListBox. Once you have to hover the boundaries and slightly left the boundaries suddenly the popup shown on the screen and remind you that MouseLeave Event is get executed. In the following code, you can be observed the above concept.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
        }
        private void checkedListBox1_MouseLeave(object sender, EventArgs e)
        {
            MessageBox.Show("Mouse is Leave !!");
        }       
    }
}
MouseLeave is the default function which you can use after the name of CheckedListBox object name. You can set any user define function also as the alternative of the default function. When you will click the button it will be inserted the values in the CheckedListBox and when you hover the mouse cursor and leave the boundaries it will be executed the MouseLeave method. If you will not click the button, in this case, MousLeave method worked also. The following image is the screenshot of output.
C# Checked ListBox
Checked ListBox BackColorChanged Event This event will occur when you will change the backColor or Background color of CheckListBox. Supposed you are wanted to perform any functionality when the user changed the backColor. Then we will code the functionality in the builtin BackColorChanged event. Instead of performing any functionality we are going to show a message box. In the following code, you can observe we have used the button to change the backColor of checkedListBox. When button will be clicked, the back color will be changed and BackColorChange Event will get executed.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.BackColor = Color.AliceBlue;
        }

        private void checkedListBox1_BackColorChanged(object sender, EventArgs e)
        {
            MessageBox.Show("BackColor Is Changed !");
        }  
    }
}
In the above code, we have inserted the values in CheckedListBox during the form initialization phase. Then we have declared the Alice Blue as the backColor of the CheckedListBox within button click event. When user will click on the button, a background color of checkedListBox is get changed and the backColorChanged event gets executed. In the following image, you can preview the output of the above code.
C# Checked ListBox
Checked ListBox ForeColorChanged Event This event is get executed when user will change the ForeColor of CheckedListBox. There are many ways to change ForeColor programmatically. Suppose we have created a button and add instruction to change the ForeColor of CheckListBox. To execute the ForeColor method we have to change the foreColor and best solution is to add foreColor change instruction into button click event. In the following code, you can be observed the above case.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.ForeColor = Color.Red;
        }

        private void checkedListBox1_ForeColorChanged(object sender, EventArgs e)
        {
            MessageBox.Show("ForeColor Is Changed !");
        }   
    }
}
In the above code, we have inserted the demo values in the initialization phase of the form. Then we have used the button click event to change the foreColor of the CheckedListBox. Then we have used message box as the functionality which is performed when ForeColor is changed. In the following image, you can observe the output of the above code.
C# Checked ListBox
Checked ListBox FontChanged Event This event will be executed when the font changed. There are several ways to change the fonts, such as you can change the font on the runtime or changed by the events. In the following code, we will create button event and write instruction to change the font the user click. Then used the message box to show interrupt when the font is getting changed. When any kind of change occurs in the font then this event will be executed, whatever you change the font size or font family. In the code, we just change the size of the font for demonstration.
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TEPNet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.Add("TEP 1");
            checkedListBox1.Items.Add("TEP 2");
            checkedListBox1.Items.Add("TEP 3");
            checkedListBox1.Items.Add("TEP 4");

        }

        private void button1_Click(object sender, EventArgs e)
        {
            checkedListBox1.Font = new Font(Font.FontFamily, 12);
        }

        private void checkedListBox1_FontChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Font Is Changed !");
        }   
    }
}
If you want to execute above code in your compiler, then you have to drag the button and CheckedListBox. Then activate the button click and FontChanged Event. Then copy paste the above code into a compiler and you will be able to execute the program. Below is the output image of above code.
C# Checked ListBox
In this tutorial, we have tried to give you a basic overview of advanced knowledge of C# Checked ListBox. All the above codes are added after the testing. You can do anything with C# Checked ListBox according to your requirements and ideas. If you face any kind of problem let us know. You can also get similar concepts from C# ListBox and C# ComboBox. You can also get the latest updates of C# Video tutorials from our YouTube Playlist (C# Video Tutorials). Don't forget to share it with your friends.