How to add check box inside combobox in c#

前端 未结 3 1432
礼貌的吻别
礼貌的吻别 2021-01-17 17: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

相关标签:
3条回答
  • 2021-01-17 18:09

    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(); 
    } 
    
    0 讨论(0)
  • 2021-01-17 18:12

    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

    0 讨论(0)
  • 2021-01-17 18:17

    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

    0 讨论(0)
提交回复
热议问题