Click here to Skip to main content
15,888,031 members
Articles / Programming Languages / C#

An OwnerDraw ComboBox with CheckBoxes in the Drop-Down

Rate me:
Please Sign up or sign in to vote.
4.74/5 (27 votes)
24 May 2007CPOL3 min read 272.2K   15.5K   64  
An OwnerDraw ComboBox with CheckBoxes in the Drop-Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CheckBoxCombo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // add three check box items to the combo box and set their checked states to true
            checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem("One", true));
            checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem("Two", true));
            checkComboBox1.Items.Add(new CheckComboBox.CheckComboBoxItem("Three", true));

            // disable the checkboxes so the user can't edit them
            checkBox1.Enabled = false;
            checkBox2.Enabled = false;
            checkBox3.Enabled = false;

            // wire up the check state changed event
            this.checkComboBox1.CheckStateChanged += new System.EventHandler(this.checkComboBox1_CheckStateChanged);

        }

        // this message handler gets called when the user checks/unchecks an item the combo box
        private void checkComboBox1_CheckStateChanged(object sender, EventArgs e)
        {
            if (sender is CheckComboBox.CheckComboBoxItem)
            {
                CheckComboBox.CheckComboBoxItem item = (CheckComboBox.CheckComboBoxItem)sender;
                switch (item.Text)
                {
                    case "One":
                        checkBox1.Checked = item.CheckState;
                        break;
                    case "Two":
                        checkBox2.Checked = item.CheckState;
                        break;
                    case "Three":
                        checkBox3.Checked = item.CheckState;
                        break;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
United States United States
Developer with over twenty years of coding for profit, and innumerable years before that of doing it at a loss.

Comments and Discussions