C# CheckListBox - Get Selected Items

| Page Views: 63

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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.




Recommendations


What do You Think


Previous Post Next Post