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.
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;
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.
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w