The following source code will provide the use of CheckedListBox Control In C#.
Source Code
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. class CheckedListBoxDemo : Form
  6. {
  7. CheckedListBox FavLangs;
  8. GroupBox grpControls;
  9. Button AddValue;
  10. Button EditValue;
  11. Button DeleteValue;
  12. Button ShowValues;
  13. TextBox OldValue;
  14. TextBox NewValue;
  15. Label OldCaption;
  16. Label NewCaption;
  17. CheckBox chkAll;
  18. public CheckedListBoxDemo()
  19. {
  20. grpControls = new GroupBox();
  21. grpControls.Text = "CheckedListBox Demo";
  22. AddValue = new Button();
  23. AddValue.Text = "&Add";
  24. AddValue.Click += new EventHandler(Add_Click);
  25. EditValue = new Button();
  26. EditValue.Text = "&Edit";
  27. EditValue.Click += new EventHandler(Edit_Click);
  28. DeleteValue = new Button();
  29. DeleteValue.Text = "&Delete";
  30. DeleteValue.Click += new EventHandler(Delete_Click);
  31. ShowValues = new Button();
  32. ShowValues.Text = "&Show";
  33. ShowValues.Click += new EventHandler(ShowValues_Click);
  34. OldValue = new TextBox();
  35. OldValue.ReadOnly = true;
  36. NewValue = new TextBox();
  37. OldCaption = new Label();
  38. OldCaption.Text = "Old Value:";
  39. NewCaption = new Label();
  40. NewCaption.Text = "New Value:";
  41. chkAll = new CheckBox();
  42. chkAll.Text = "Check/UnCheck All";
  43. chkAll.CheckedChanged += new EventHandler(Checked_Changed);
  44. chkAll.Width = 175;
  45. OldCaption.Location = new Point(15, 15);
  46. PositionControl(OldCaption, OldValue, true);
  47. PositionControl(OldCaption, NewCaption, false);
  48. PositionControl(OldValue, NewValue, false);
  49. PositionControl(NewCaption, AddValue, false);
  50. PositionControl(AddValue, EditValue, true);
  51. PositionControl(EditValue, DeleteValue, true);
  52. PositionControl(DeleteValue, ShowValues, true);
  53. PositionControl(AddValue, chkAll, false);
  54. grpControls.Controls.AddRange(new Control[] { OldCaption, OldValue, NewCaption, NewValue, AddValue, EditValue, DeleteValue, ShowValues, chkAll });
  55. grpControls.Size = new Size(450, 200);
  56. FavLangs = new CheckedListBox();
  57. FavLangs.Location = new Point(10, 10);
  58. FavLangs.SelectedIndexChanged += new EventHandler(SelectedIndex_Changed);
  59. grpControls.Location = new Point(FavLangs.Left + FavLangs.Width + 20, FavLangs.Top);
  60. this.Controls.AddRange(new Control[] { FavLangs, grpControls });
  61. }
  62. private void PositionControl(Control source, Control destination, bool CanPlaceHorizontal)
  63. {
  64. if (CanPlaceHorizontal)
  65. {
  66. destination.Location = new Point(source.Left + source.Width + 20, source.Top);
  67. }
  68. else
  69. {
  70. destination.Location = new Point(source.Left, source.Top + source.Height + 20);
  71. }
  72. }
  73. private void Add_Click(object sender, EventArgs e)
  74. {
  75. if (NewValue.Text.Trim() != "")
  76. {
  77. FavLangs.Items.Add(NewValue.Text);
  78. }
  79. else
  80. {
  81. MessageBox.Show("Enter a Value to Add");
  82. }
  83. }
  84. private void SelectedIndex_Changed(object sender, EventArgs e)
  85. {
  86. OldValue.Text = FavLangs.Items[FavLangs.SelectedIndex].ToString();
  87. }
  88. private void Edit_Click(object sender, EventArgs e)
  89. {
  90. if (FavLangs.SelectedIndex == -1)
  91. {
  92. MessageBox.Show("Select a Item to Edit");
  93. }
  94. else
  95. {
  96. if (NewValue.Text.Trim() != "")
  97. {
  98. FavLangs.Items[FavLangs.SelectedIndex] = NewValue.Text;
  99. }
  100. else
  101. {
  102. MessageBox.Show("Enter a Value to Edit");
  103. }
  104. }
  105. }
  106. private void Delete_Click(object sender, EventArgs e)
  107. {
  108. if (FavLangs.SelectedIndex != -1)
  109. {
  110. FavLangs.Items.RemoveAt(FavLangs.SelectedIndex);
  111. }
  112. else
  113. {
  114. MessageBox.Show("Select a Item to Delete");
  115. }
  116. }
  117. private void ShowValues_Click(object sender, EventArgs e)
  118. {
  119. string SelectedValues = "The following value(s) are Selected:\n" + new String('-', 48) + "\n";
  120. for (int i = 0; i < FavLangs.CheckedItems.Count; i++)
  121. {
  122. SelectedValues += FavLangs.CheckedItems[i].ToString() + "\n";
  123. }
  124. MessageBox.Show(SelectedValues);
  125. }
  126. private void Checked_Changed(object sender, EventArgs e)
  127. {
  128. for (int i = 0; i < FavLangs.Items.Count; i++)
  129. {
  130. FavLangs.SetItemChecked(i, chkAll.Checked);
  131. }
  132. }
  133. public static void Main()
  134. {
  135. Application.Run(new CheckedListBoxDemo());
  136. }
  137. }