Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 630
Vyasdev Meledath Avatar asked May 11 '11 07:05

Vyasdev Meledath


People also ask

How do I add options to my ComboBox?

To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.

What is ComboBox and checkbox?

Check boxes are labeled options that allow the user to select multiple options at once. Thus, any, all, or none of a set of check boxes may be selected. Combo boxes are drop-down menus that can appear along with other widgets in the main area of a window.

Which method is used to add items in ComboBox even?

To add a set of items to the combo box it is best to use the AddRange method.


3 Answers

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

like image 52
Larry Avatar answered Oct 13 '22 17:10

Larry


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(); 
} 
like image 28
Mick Bruno Avatar answered Oct 13 '22 15:10

Mick Bruno


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

like image 42
Allender Avatar answered Oct 13 '22 17:10

Allender