How to add check box inside combobox in c#
I want to add check box inside comboBox in C#. My purpose is that the user can select开发者_开发技巧 multiple values from one ComboBox ( Check all and Uncheck all ).
Please Help
You have to extend the ComboBox control by providing your own rendering strategy, and "manually" adding a CheckBox.
Theses open source project are ready to use :
http://www.codeproject.com/KB/combobox/CheckComboBox.aspx http://www.codeproject.com/KB/combobox/extending_combobox.aspx
It is a wrong usage of a ComboBox control, because the user has no possibility to see his choices. For multiple selection, I recommend you to consider this CheckedListBox control:
link to MSDN
There is an ASP.NET open source control at http://dropdowncheckboxes.codeplex.com/ that I've used and been very happy with. There is also a WinForms open source control at http://www.codeproject.com/KB/combobox/extending_combobox.aspx that doesn't look quite as strong but maybe somebody could combine the best of both. If well implemented this is really a great addition to your toolkit. The above 2 implementations show all of the items selected and give you a number of related checkboxes in a reduced area and with excellent grouping. My addition to the ASP.NET version was to allow a list of checked files to use just file names instead of full paths if this gets too long. See above link for full code. Below is just my addition which is called instead of UpdateSelection in your postback handler:
// Update the caption assuming that the items are files
// If the caption is too long, eliminate paths from file names
public void UpdateSelectionFiles(int maxChars) {
StringBuilder full = new StringBuilder();
StringBuilder shorter = new StringBuilder();
foreach (ListItem item in Items) {
if (item.Selected) {
full.AppendFormat("{0}; ", item.Text);
shorter.AppendFormat("{0}; ", new FileInfo(item.Text).Name);
}
}
if (full.Length == 0) Texts.SelectBoxCaption = "Select...";
else if (full.Length <= maxChars) Texts.SelectBoxCaption = full.ToString();
else Texts.SelectBoxCaption = shorter.ToString();
}
精彩评论