SIGN UP MEMBER LOGIN:    
ARTICLE

Checked ListBox Control In C#

Posted by Prasad H Articles | Windows Forms C# October 18, 2001
The following source code will provide the use of CheckedListBox Control In C#.
Reader Level:

The following source code will provide the use of CheckedListBox Control In C#.

Source Code:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

class CheckedListBoxDemo:Form
{
CheckedListBox FavLangs;
GroupBox grpControls;
Button AddValue;
Button EditValue;
Button DeleteValue;
Button ShowValues;
TextBox OldValue;
TextBox NewValue;
Label OldCaption;
Label NewCaption;
CheckBox chkAll;

public CheckedListBoxDemo()
{
grpControls=
new GroupBox();
grpControls.Text="CheckedListBox Demo";
AddValue=
new Button();
AddValue.Text="&Add";
AddValue.Click+=
new EventHandler(Add_Click);
EditValue=
new Button();
EditValue.Text="&Edit";
EditValue.Click+=
new EventHandler(Edit_Click);
DeleteValue=
new Button();
DeleteValue.Text="&Delete";
DeleteValue.Click+=
new EventHandler(Delete_Click);
ShowValues=
new Button();
ShowValues.Text="&Show";
ShowValues.Click+=
new EventHandler(ShowValues_Click);
OldValue=
new TextBox();
OldValue.ReadOnly=
true;
NewValue=
new TextBox();
OldCaption=
new Label();
OldCaption.Text="Old Value:";
NewCaption=
new Label();
NewCaption.Text="New Value:";
chkAll=
new CheckBox();
chkAll.Text="Check/UnCheck All";
chkAll.CheckedChanged+=
new EventHandler(Checked_Changed);
chkAll.Width=175;
OldCaption.Location=
new Point(15,15);
PositionControl(OldCaption,OldValue,
true);
PositionControl(OldCaption,NewCaption,
false);
PositionControl(OldValue,NewValue,
false);
PositionControl(NewCaption,AddValue,
false);
PositionControl(AddValue,EditValue,
true);
PositionControl(EditValue,DeleteValue,
true);
PositionControl(DeleteValue,ShowValues,
true);
PositionControl(AddValue,chkAll,
false);
grpControls.Controls.AddRange(
new Control[]{OldCaption,OldValue,NewCaption,NewValue,AddValue,EditValue,DeleteValue,ShowValues,chkAll});
grpControls.Size=
new Size(450,200);
FavLangs=
new CheckedListBox();
FavLangs.Location=
new Point(10,10);
FavLangs.SelectedIndexChanged+=
new EventHandler(SelectedIndex_Changed);
grpControls.Location=
new Point(FavLangs.Left+FavLangs.Width+20,FavLangs.Top);
this.Controls.AddRange(new Control[]{FavLangs,grpControls});
}

private void PositionControl(Control source,Control destination,bool CanPlaceHorizontal)
{
if(CanPlaceHorizontal)
{
destination.Location=
new Point(source.Left+source.Width+20,source.Top);
}
else
{
destination.Location=
new Point(source.Left,source.Top+source.Height+20);
}
}

private void Add_Click(object sender,EventArgs e)
{
if(NewValue.Text.Trim()!="")
{
FavLangs.Items.Add(NewValue.Text);
}
else
{
MessageBox.Show("Enter a Value to Add");
}
}

private void SelectedIndex_Changed(object sender,EventArgs e)
{
OldValue.Text=FavLangs.Items[FavLangs.SelectedIndex].ToString();
}

private void Edit_Click(object sender,EventArgs e)
{
if(FavLangs.SelectedIndex==-1)
{
MessageBox.Show("Select a Item to Edit");
}
else
{
if(NewValue.Text.Trim()!="")
{
FavLangs.Items[FavLangs.SelectedIndex]=NewValue.Text;
}
else
{
MessageBox.Show("Enter a Value to Edit");
}
}
}

private void Delete_Click(object sender,EventArgs e)
{
if(FavLangs.SelectedIndex!=-1)
{
FavLangs.Items.RemoveAt(FavLangs.SelectedIndex);
}
else
{
MessageBox.Show("Select a Item to Delete");
}
}

private void ShowValues_Click(object sender,EventArgs e)
{
string SelectedValues="The following value(s) are Selected:\n" + new String('-',48) + "\n";
for(int i=0;i<FavLangs.CheckedItems.Count;i++)
{
SelectedValues+=FavLangs.CheckedItems[i].ToString() + "\n";
}
MessageBox.Show(SelectedValues);
}

private void Checked_Changed(object sender,EventArgs e)
{
for(int i=0;i<FavLangs.Items.Count;i++)
{
FavLangs.SetItemChecked(i,chkAll.Checked);
}
}

public static void Main()
{
Application.Run(
new CheckedListBoxDemo());
}
}

share this article :
post comment
 

dfgsdfsdg sdfgdfgdf sdfgsdfgsdfg sfdgsfgsdfgfgf sdfg sgfgfsdfg sdddffffggsdrgrg aaaadfgdfg adwerwerwer afdasdghgrrsdd wrgrrfwd asdfg qwer rtyery sdfasd bnvbnm ghhhh lllllkj assaa aaaaaaaaaaaa aweeerrrrt erterter fgsfsdfgdfg sgsgsdfg seeee sdfgsd

Posted by pradeep kumar Dec 16, 2010

I tried to do it but i can't wil u please help me out by sending me demo project thanku

Posted by kalpana reddy Apr 01, 2009

I imported the code into my Visual Studio 2008 for C#. I used template Windows Forms and hit the "Build" button. After testing a little I have no errors. There were none to start with, but just wanted to be sure! I have book-marked this http address and notice that your site promises support with all things .NET Thanks! --John

Posted by John deCoville Dec 29, 2008
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor