Friday 31 January 2014

CheckedCombobox in winforms C#

In this article we are going to see how to create a checkbox in the combobox item, For this we have to create a template for the data .

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;

namespace CustomControl
{
      class CheckComboBox:ComboBox
    {               
        public class ComboboxData
        {            
             string TotalData;

            private bool _checked;
            public bool Checked
            {
                set { _checked = value; }
                get { return _checked;  }
            }

            private string _data;
            public string Data
            {
                set { _data = value; }
                get { return _data;  }
            }

            public ComboboxData(string value,bool ischeck)
            {
                Data = value;
                Checked = ischeck;              
            }

            public override string ToString()
            {               
                return Data;
            }


        }

        public event EventHandler Checkchanged;

        public CheckComboBox()
        {
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        }

        public List<ComboboxData> CheckItems
        {
            get
            {

                List<ComboboxData> newitems = new List<ComboboxData>();
                foreach (var item in Items)
                {
                    if (item is ComboboxData)
                    {
                        newitems.Add(item as ComboboxData);
                    }
                }
                return newitems;

            }
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            ComboboxData data = (ComboboxData)SelectedItem;
            data.Checked = !data.Checked;

            if (Checkchanged != null) {
                Checkchanged(data, e);
            }


        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (e.Index == -1)
            {
                return;
            }

            if (Items[e.Index] is ComboboxData)
            {
                ComboboxData data = Items[e.Index] as ComboboxData;
                CheckBoxRenderer.RenderMatchingApplicationState = true;
                CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.Bounds.X, e.Bounds.Y),                      e.Bounds, data.Data, this.Font,
                    (e.State & DrawItemState.Focus) == 0, data.Checked ?                                          CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);
            }
            else
            {
                e.Graphics.DrawString(Items[e.Index].ToString(), this.Font, Brushes.Black,                          new Point(e.Bounds.X, e.Bounds.Y));
                return;
            }

            base.OnDrawItem(e);
        }
   
    }
}



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

            checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Rajesh", true));
            checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Suresh", false));
            checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Ram", true));
            checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Ravi", true));


            checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("1", true));
            checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("2", false));
            checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("3", true));
            checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("4", true));


        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<CheckComboBox.ComboboxData> dat1 = checkComboBox1.CheckItems;
            var data = new CheckComboBox.ComboboxData("bnm", true);           
            var sdata =data.ToString();
        }
    }
}




Output:




From this post you can learn how to create a Custom Combobox.

34 comments:

  1. Hey, almost perfect but ToString is returning wrong values and the static modifier of datas variable is causing a lot of problems with the List, a lot of items inside the list and you cant use more than one combobox in a the same form....

    ReplyDelete
  2. Hi ,

    Based on your indication, i changed the code slightly, with one Feature enabled in the CheckComboBox class, and modified the ToString method, with some change in the code of static variables, and sharing features between the instances..... Following are the changed code ..

    New Property added : CheckItems
    Which will give the items present in the combobox.

    ToString method gives you the string of that variable.

    You can get the data by the following sample code after modification >>>>>>>>


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

    checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Rajesh", true));
    checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Suresh", false));
    checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Ram", true));
    checkComboBox1.Items.Add(new CheckComboBox.ComboboxData("Ravi", true));


    checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("1", true));
    checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("2", false));
    checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("3", true));
    checkComboBox2.Items.Add(new CheckComboBox.ComboboxData("4", true));


    }

    private void button1_Click(object sender, EventArgs e)
    {
    List dat1 = checkComboBox1.CheckItems;
    var data = new CheckComboBox.ComboboxData("bnm", true);
    var sdata =data.ToString();
    }
    }
    }

    ReplyDelete
  3. Hi there great site, what is this declared as - checkComboBox1- as I cant put this line in because I havnt declared it.I tried combobox and checklistbox but then I cant put this line in - List dat1 = checkComboBox1.CheckItems;

    ReplyDelete
    Replies
    1. When you build the above code , it will give you the control in the toolbox with the name of CheckComboBox, you have to just drag and drop the control from toolbox to the Forms, then start coding in it, In the above sample i just drag and dropped the two controls in the forms, you can see the declaration in the designer file, actually i didn't share the designer code for this post

      Delete
    2. Thank you Raj Esh, this code helped me alot!One more question though, my control doesnt show the selections when i select more than one like in your example it shows Rajesh,Suresh,Ram - but in mine no matter how many i select i only see one name.Also what is button1 supposed to be?Final update button?

      Delete
    3. Hi Raj
      Ok I can live without the box showing all selected items.But Im using this code to do 3 boxes that have to fire each other when ever selectionchangecommited, but the problem is that checked only becomes true once selectionchangecommited is done,meaning I cant ever get the updated value.I removed all the auto events and put manual fire events to solve it.Thanks for your code and help:)

      Mr J Singh

      Delete
    4. Thank you Raj. One more question, my control doesn't show the selections when i select more than one like in your example it shows Rajesh,Suresh,Ram - but in mine no matter how many i select i only see one name.

      Delete
  4. Hi, i want to ask how to check, whether the checkcombobox is check or not?

    ReplyDelete
  5. How to implement search and select the required check box automatically

    ReplyDelete
  6. Instead of closing the drop down on selection is there any possibility to select multiple

    ReplyDelete
  7. Hi , building the code by creating a new class, Everything in my toolbox disappears. If i delete the class, toolbox items appear again. Running vs2017. Am i doing something wrong?

    ReplyDelete
  8. Hi, When we select any items from combobox with checkboxes the form loses its focus.
    Please suggest how to set the focus to main form.

    Thanks.

    ReplyDelete
  9. combobox with checkbox is a required field . But despite setting the required property as true and also the property backcolor with System.Drawing.SystemColors.Window; the field doesn't display in the the shade of yellow which basically indicates its a required field .

    ReplyDelete
  10. when selecting the item the control flashes returns to initialize?

    ReplyDelete