Introduction
This post shows you some basic code for the checklistbox and the listbox.
Background
With this source, you can check the items that you have selected.
Using the Code
Here is the sample code of the small project. You can even download the source code.
The below code is for Form1
where you can set the check items:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
list_Chk_Box.Add("Bagmati");
}
public string index;
private void Form1_Load(object sender, EventArgs e)
{
if (index != null)
{
string[] StrIndex = index.Split(',');
for (int j = 0; j < StrIndex.Length - 1; j++)
{
list_Chk_box.SetItemChecked
(Convert.ToInt32(StrIndex[j]), true);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
foreach (string str in list_Chk_box.CheckedItems)
{
frm2.str += str + ",";
}
foreach (int index in list_Chk_box.CheckedIndices)
{
frm2.str_index += index + ",";
}
frm2.Show();
This.Hide();
}
}
The below code is for Form2
where you see the items selected from Form1
.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string str;
public string str_index;
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.index = str_index;
frm1.Show();
this.Hide();
}
private void Form2_Load(object sender, EventArgs e)
{
if (str != null)
{
string[] strarray = str.Split(',');
for (int i = 0; i < strarray.Length - 1; i++)
{
listBox1.Items.Add(strarray[i]);
}
}
}
}
Now after you select Back, you can find the same item selected/checked.
Points of Interest
With this source, you can learn the related events of the checklist box. These are the basic steps for the check list box.