Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#
Tip/Trick

CheckList Box Tips

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
9 May 2011CPOL 15.9K   216   5   3
How to set the check status of the checklist box?
Sample Image - maximum width is 600 pixels

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:

C#
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.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Nepal Nepal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralTypo Pin
Ravi Bhavnani10-May-11 6:50
professionalRavi Bhavnani10-May-11 6:50 
GeneralRe: Typo Pin
PeacefulPete31-May-11 22:56
PeacefulPete31-May-11 22:56 
GeneralMy vote of 5 Pin
liveraghabendra10-May-11 4:09
liveraghabendra10-May-11 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.