III.3- Using Checked
Listbox Control
The CheckedListBox control gives you all the capability of a list box and also allows you to display a check mark next to the items in the list box.
Check out the example below:
Source Code:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; 1 < 11; i++)
list1.Items.Add("item" + i.ToString());
}
private void chkSelAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < list1.Items.Count; i++)
list1.SetItemChecked(i,
chkSelAll.Checked);
}
private void chkRemoveAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < list2.Items.Count; i++)
list2.SetItemChecked(i,
chkRemoveAll.Checked);
}
private void btnAdd_Click(object sender, EventArgs e)
{
for (int i =
list1.Items.Count -1; i>-1; i--)
{
if(list1.GetItemChecked(i) == true)
{
list2.Items.Add(list1.Items[i].ToString());
list1.Items.RemoveAt(i);
}
}
list2.Sorted = true;
}
private void btnRemove_Click(object sender, EventArgs e)
{
for(int i =list2.Items.Count-1; i> -1; i--)
{
if (list2.GetItemChecked(i) == true)
{
list2.Items.Add(list2.Items[i].ToString());
list2.Items.RemoveAt(i);
}
}
list1.Sorted = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
0 Comments