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.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post