3

Could you help me out doing this? How to select multiple values in combo box having dropdown style as dropdown in windows forms?

UserRA
  • 185
  • 1
  • 2
  • 16
  • possible duplicate of [ComboBox with CheckBoxes in WinForms](http://stackoverflow.com/questions/9214564/combobox-with-checkboxes-in-winforms) – tafa Jul 15 '15 at 11:23
  • cant we do it with out using check box? is there a way to select multiple values from dropdown and append to the combo box? @tafa – UserRA Jul 15 '15 at 11:37

1 Answers1

3

You can populate your ComboBox items with CheckBoxes, then do something in code like the example below to keep the SelectedIndex at -1 so it becomes just a drop down list and improves consistency to the user. Also having it check whats been ticked once the drop-down is closed or when the user interacts with any other element after selecting the preferences.

This assumes you've named your ComboBox "cbList" and you have already populated it with three CheckBoxes names "one", "two" and ""three.

private void cbList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    cbList.SelectedIndex = -1;
}

private void cbList_DropDownClosed(object sender, EventArgs e)
{
    foreach(CheckBox chk in cbList.Items){
        if(chk.IsChecked.HasValue && chk.IsChecked.Value){
            switch (chk.Content.ToString()) { 
                case "one":
                    // Do something
                    break;
                case "two":
                    // Do something
                    break;
                case "three":
                    // Do something
                    break;
            }
        }
    }
}

You could also make the first element a ComboBoxItem with the text "Please select all that apply.." or something and have the SelectedIndex always be set to 0.

Hope this helps.

Sam
  • 350
  • 2
  • 9
  • I don't want to use check boxes, can you suggest me some answers to select multiple values among the values in the drop down and append it to the combo box? @sp10acnFIFO – UserRA Jul 15 '15 at 12:00
  • @UserAR You may be able to do something like changing the background colour of the content for each item when they are selected or deselected then check the hex values for every items background color to do the logic you want. However every time you select an item to highlight it the drop down would close. If you can find a way of preventing the ComboBox from closing this may be your best shot. If not all I can offer is use the method above or use a ListBox. – Sam Jul 15 '15 at 12:36