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());
- }
- }
pradeep kumarPosted Dec 16, 2010, 10:19 AM
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
kalpana reddyPosted Apr 1, 2009, 9:18 AM
I tried to do it but i can't wil u please help me out by sending me demo project thanku
John deCovillePosted Dec 29, 2008, 2:50 PM
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