C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: From here, you can right-click on the CheckedListBox and select Properties to adjust properties and also add event handlers.
Here: In this first example, you need to add the Form1_Load event handler by double-clicking on the form as well.
EventC# program that adds items to CheckedListBox
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var items = checkedListBox1.Items;
items.Add("Perls");
items.Add("Checked", true);
}
}
}
And: The CheckedListBox will instantly detect the new items and display them. This makes programs responsive to user actions.
Tip: You must check the SelectedIndex property for the value -1 to prevent an IndexOfOutRangeException.
IndexOutOfRangeExceptionC# program that uses SelectedIndex property
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get selected index, and then make sure it is valid.
int selected = checkedListBox1.SelectedIndex;
if (selected != -1)
{
this.Text = checkedListBox1.Items[selected].ToString();
}
}
}
}
And: This functionality is the same in Visual Studio 2010 as well. Use a line break after each separate item.
Note: Please see the detailed article on IntegralHeight. This property may improve the design of your program.