C# CheckListBox – Get Selected Items

Here guys we see how to work with a CheckListBox.We select items and add all of them to a richtextbox on button click.We also Multi-Unselect all items on button click,clearing our richtextbox.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_CHKLBOX_SELECTED
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void getCheckedItems()
    {
      richTextBox1.Text = "";
      //LOOP THRU CHKED ITEMS
      foreach (String item in checkedListBox1.CheckedItems)
      {

        //ADD TO RICHTEXTBOX
        richTextBox1.Text += item + ",n";
      }
    }

    //UNCHECK ALL ITEMS
    private void unCheck()
    {
      for(int i=0;i<checkedListBox1.Items.Count;i++)
      {
        checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
      }

      //CLEAR RICHTEXTBOX
      richTextBox1.Text = "";
    }

    private void getBtn_Click(object sender, EventArgs e)
    {
      getCheckedItems();
    }

    private void unselectBtn_Click(object sender, EventArgs e)
    {
      unCheck();
    }

  }
}

Cheers.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *