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.