How to add check box inside combobox in c#

有些话、适合烂在心里 提交于 2020-05-14 16:23:27

问题


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


回答1:


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




回答2:


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




回答3:


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(); 
} 


来源:https://stackoverflow.com/questions/5960455/how-to-add-check-box-inside-combobox-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!