csharp

Check Uncheck all CheckBoxes in CheckedListBox in Windows Application using C#

Here LaxmiKant has explained with an example, Check Uncheck all CheckBoxes in CheckedListBox in Windows Application using C#

The check uncheck all functionality will be done using an additional CheckBox that will act as Check All CheckBox for the CheckedListBox in Windows Forms (WinForms) Application using C#.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

In this article I will explain with an example, Check Uncheck all CheckBoxes in CheckedListBox in Windows Application using C#.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 1: Open visual studio 2017 and click on Project.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Ste 2: Select Windows Form Application and write the name of Application.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 3: After that Drag and Drop CheckedListBox, CheckBox from ToolBox.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 4: Open properties of CheckedListBox and change the name of CheckedListBox and generate SelectIndexChanged event.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 5: Open Properties of CheckBox and change the name of CheckBox and generate Click event.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 6: Click On Edit Items button and Enter the Strings in the collection after that click on “OK” button.

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Form Design

check-uncheck-checkboxes-checkedlistbox-windows-application-using-c

Step 7: After Design the page, Open Form1.cs page and write this code for Check Uncheck all CheckBoxes in CheckedListBox when click on Select All CheckBox is checked

 private void checkAll_Click_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedHobbies.Items.Count; i++)
            {
                checkedHobbies.SetItemChecked(i, checkAll_Click.Checked);
            }
        }

Write this code for Check Uncheck Check All CheckBox when other CheckBoxes in CheckedListBox are checked or unchecked
When any CheckBox of the CheckedListBox control is checked or unchecked, the SelectedIndexChanged event handler is executed.

 private void checkedHobbies_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isAllChecked = true;
            for (int i = 0; i < checkedHobbies.Items.Count; i++)
            {
                if (!checkedHobbies.GetItemChecked(i))
                {
                    isAllChecked = false;
                    break;
                }
            }

            checkAll_Click.Checked = isAllChecked;

        }

Complete Code on Form1.cs page

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 CheckUncheckDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void checkAll_Click_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedHobbies.Items.Count; i++)
            {
                checkedHobbies.SetItemChecked(i, checkAll_Click.Checked);
            }
        }

        private void checkedHobbies_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isAllChecked = true;
            for (int i = 0; i < checkedHobbies.Items.Count; i++)
            {
                if (!checkedHobbies.GetItemChecked(i))
                {
                    isAllChecked = false;
                    break;
                }
            }

            checkAll_Click.Checked = isAllChecked;

        }
    }
}

Leave a Reply

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