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.
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....
ReplyDeleteHi ,
ReplyDeleteI will check the code .
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?
DeleteHi Raj
DeleteOk 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
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.
DeleteHi, i want to ask how to check, whether the checkcombobox is check or not?
ReplyDeletevery benefial for me
ReplyDeletegood to hear
DeleteHow 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
ReplyDeleteHi , 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?
ReplyDeleteHi, When we select any items from combobox with checkboxes the form loses its focus.
ReplyDeletePlease suggest how to set the focus to main form.
Thanks.
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 .
ReplyDeletewhen selecting the item the control flashes returns to initialize?
ReplyDeletevan
ReplyDeletekastamonu
elazığ
tokat
sakarya
FANB
kars
ReplyDeletesinop
sakarya
ankara
çorum
00UX0J
van
ReplyDeletekastamonu
elazığ
tokat
sakarya
6TG
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
318MÄ°
erzurum evden eve nakliyat
ReplyDeleteburdur evden eve nakliyat
antalya evden eve nakliyat
denizli evden eve nakliyat
ısparta evden eve nakliyat
SAW2CH
BBFC3
ReplyDeleteTekirdaÄŸ Evden Eve Nakliyat
Bartın Evden Eve Nakliyat
Çorum Evden Eve Nakliyat
Osmaniye Evden Eve Nakliyat
Edirne Evden Eve Nakliyat
D7AB6
ReplyDeleteParibu Güvenilir mi
Kilis Evden Eve Nakliyat
Rize Evden Eve Nakliyat
Aydın Evden Eve Nakliyat
Tunceli Evden Eve Nakliyat
C7C76
ReplyDeleteGümüşhane Şehirler Arası Nakliyat
İstanbul Şehir İçi Nakliyat
Bilecik Şehir İçi Nakliyat
Ünye Parke Ustası
Manisa Şehir İçi Nakliyat
Adana Lojistik
Poloniex Güvenilir mi
Silivri Fayans Ustası
Mersin Evden Eve Nakliyat
79AAE
ReplyDeleteGümüşhane Parça Eşya Taşıma
AAX Güvenilir mi
Niğde Şehirler Arası Nakliyat
Ãœnye Oto Lastik
Bayburt Şehir İçi Nakliyat
Bilecik Evden Eve Nakliyat
Tekirdağ Fayans Ustası
Kırklareli Evden Eve Nakliyat
Karabük Şehirler Arası Nakliyat
2EC97
ReplyDeleteSivas Lojistik
İzmir Şehir İçi Nakliyat
Keçiören Boya Ustası
Aksaray Şehir İçi Nakliyat
Iğdır Evden Eve Nakliyat
Sinop Şehirler Arası Nakliyat
Kırklareli Lojistik
Eskişehir Şehirler Arası Nakliyat
Çorum Evden Eve Nakliyat
02CB7
ReplyDeleteIsparta Parça Eşya Taşıma
Jns Coin Hangi Borsada
Bartın Evden Eve Nakliyat
Artvin Parça Eşya Taşıma
Hatay Evden Eve Nakliyat
Gümüşhane Parça Eşya Taşıma
Kocaeli Evden Eve Nakliyat
Sakarya Evden Eve Nakliyat
Antalya Rent A Car
23BC2
ReplyDeletetestosterone enanthate for sale
Ankara Asansör Tamiri
Adana Evden Eve Nakliyat
parabolan for sale
testosterone enanthate
order oxandrolone anavar
Kocaeli Evden Eve Nakliyat
dianabol methandienone
buy boldenone
F8B16
ReplyDeleteorder oxandrolone anavar
peptides
order sarms
order primobolan
parabolan
buy dianabol methandienone
boldenone
buy testosterone propionat
fat burner for sale
D765F
ReplyDeletebinance %20 referans kodu
2A943
ReplyDeletereferanskodunedir.com.tr
50411
ReplyDeleteyozgat canlı sohbet bedava
erzurum canli sohbet chat
ordu görüntülü sohbet ücretsiz
sakarya ucretsiz sohbet
çorum sohbet sitesi
telefonda rastgele sohbet
tekirdağ kızlarla rastgele sohbet
kayseri görüntülü sohbet sitesi
amasya mobil sesli sohbet
52C91
ReplyDeleteuniswap
poocoin
eigenlayer
onekey
shiba
dappradar
shiba
chainlist
safepal