Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one CheckedListBox(CheckedListBox1) and one CheckBox(CheckBox1).

I want to show all the checked items in a same CheckedListBox1,
suppose when checkBox1 is checked then show which items were checked in CheckedListBox1, and if checkbox is unchecked then show all items(which checked before and rest of all).


Example of what i want to do :

CheckedListBox1 List Items are :
"A" unchecked
"B" Checked
"C" Checked
"D" Unchecked

if Checkbox is checked then shows only checked items from CheckedListbox1 to CheckedListbox1.
Here the items are :
"B" and "C"

if checkbox is unchecked then shows all items which were in CheckdeListBox1.
Here the items are :
"A" unchecked
"B" Checked
"C" Checked
"D" Unchecked

I saw it in VB6 Add component dialog Box, where all the component was in one checkedlistbox, and there was a checkbox(which text is "Selected Items only").if we checked that checkbox then it will shows only checked items in checkedlistbox.


How to do this??


thanks in Advanced!!!

Regards :
Jayanta.
Posted
Updated 10-Feb-14 3:19am
v3

try this

C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

            bool flag = (sender as CheckBox).Checked;
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
                checkedListBox1.SetItemChecked(i, flag);

        }
 
Share this answer
 
Comments
BillWoodruff 10-Feb-14 8:39am    
+5 Lean, clean, and mean.
Karthik_Mahalingam 10-Feb-14 8:40am    
Thanks Bill:)
JayantaChatterjee 10-Feb-14 8:43am    
Sir, Sorry for My English language..

I want to show only checked Items from checkedListBox, means when checkBox is checked then show which items were checked in CheckedListBox, and if checkbox is unchecked then show all items(which checked before and rest of all)...
Karthik_Mahalingam 10-Feb-14 8:44am    
show in what ?
label , textbox ??
JayantaChatterjee 10-Feb-14 8:45am    
show in same CheckedListBox1.
here what you want:

// file: MyCheckListBox 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_Checklist
{
    public class MyCheckListBox : System.Windows.Forms.CheckedListBox
    {
        List<object> all = new List<object>();
        List<int> chks = new List<int>();
        bool showChecked = false;

        public MyCheckListBox()
            : base()
        {
            this.ItemCheck += MyCheckListBox_ItemCheck;
        }

        void MyCheckListBox_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
        {
            if (!this.showChecked)
                return;

            var o = this.Items[e.Index];
            int oi = all.IndexOf(o);
            if (e.NewValue == System.Windows.Forms.CheckState.Checked)
                chks.Add(oi);
            else
                chks.Remove(oi);

        }

        public void ShowOnlyChecked(bool val)
        {
            this.showChecked = val;

            if (val)
            {
                all.AddRange(this.Items.Cast<object>());
                chks.AddRange(this.CheckedIndices.Cast<int>());

                for (int i = 0; i < all.Count; i++)
                    if (!chks.Contains(i))
                        this.Items.Remove(all[i]);
            }
            else
            {
                this.Items.Clear();
                this.Items.AddRange(this.all.ToArray());
                this.chks.ForEach(i1 => this.SetItemChecked(i1, true));

                all.Clear();
                chks.Clear();
            }
        }


    }
}



and an example form:
C#
// file: Form1.cs
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 Test_Checklist
{
    public partial class Form1 : Form
    {
        MyCheckListBox lb = new MyCheckListBox();

        public Form1()
        {
            InitializeComponent();

            this.SuspendLayout();
            this.Controls.Add(lb);
            this.lb.Items.AddRange(new string[] { "item1", "item2", "item3" });
            this.lb.Left = this.checkBox1.Left;
            this.lb.Top = this.checkBox1.Top + 20;

            this.ResumeLayout(false);


            this.checkBox1.CheckState = CheckState.Unchecked;
            this.checkBox1.CheckedChanged += checkBox1_CheckedChanged;
        }

        void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            switch (this.checkBox1.CheckState)
            {
                case CheckState.Checked:
                    this.lb.ShowOnlyChecked(true);
                    break;
                case CheckState.Indeterminate:
                case CheckState.Unchecked:
                    this.lb.ShowOnlyChecked(false);
                    break;
                default:
                    break;
            }
        }
    }


}



It doesn't have designer support, but you can convert it into custom control.
 
Share this answer
 
Comments
JayantaChatterjee 10-Feb-14 9:50am    
My vote of 5++

Thank you Sir...
Thanks a Lottttttttttttttttt...
its perfect .... :-)
Vedat Ozan Oner 10-Feb-14 10:00am    
you are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900