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#:
Form.cs:
************
Output:
From this post you can learn how to create a Custom Combobox.
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();
}
}
}
From this post you can learn how to create a Custom Combobox.


Hi ,
ReplyDeleteBased 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();
}
}
}
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;
ReplyDeleteWhen 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
DeleteThank 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?
Deletevery benefial for me
ReplyDeleteHow to implement search and select the required check box automatically
ReplyDeleteInstead of closing the drop down on selection is there any possibility to select multiple
ReplyDelete