C# Checked ListBox Control

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 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
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.
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
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.
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.
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.
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.
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.
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.
×
![]()





































































