views:

538

answers:

5

What's the best way of implementing a multiple choice option in Windows Forms? I want to enforce a single selection from a list, starting with a default value.

It seems like a ComboBox would be a good choice, but is there a way to specify a non-blank default value? I could just set it in the code at some appropriate initialisation point, but I feel like I'm missing something.

+2  A: 

You should be able to just set the ComboBox.SelectedIndex property with what you want the default value to be.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx

Rob
+2  A: 

Use the ComboBox.SelectedItem or SelectedIndex property after the items have been inserted to select the default item.

You could also consider using RadioButton control to enforce selection of a single option.

Yaakov Ellis
+6  A: 

If you only want one answer from the group, then a RadioButton control would be your best fit or you could use the ComboBox if you will have a lot of options. To set a default value, just add the item to the ComboBox's collection and set the SelectedIndex or SelectedItem to that item.

Depending on how many options you are looking at, you can use a ListBox with the SelectionMode property set to MultiSimple, if it will be multiple choice or you could use the CheckBox control.

Dale Ragan
+2  A: 

You can use a ComboBox with the DropDownStyle property set to DropDownList and SelectedIndex to 0 (or whatever the default item is). This will force always having an item from the list selected. If you forget to do that, the user could just type something else into the edit box part - which would be bad :)

Wilka
+1  A: 

If you are giving the user a small list of choices then stick with the radio buttons. However, if you will want want to use the combo box for dynamic or long lists. Set the style to DropDownList.

private sub populateList( items as List(of UserChoices))
   dim choices as UserChoices
   dim defaultChoice as UserChoices 

   for each choice in items
      cboList.items.add(choice)
      '-- you could do user specific check or base it on some other 
      '---- setting to find the default choice here
      if choice.state = _user.State or choice.state = _settings.defaultState then 
          defaultChoice = choice
      end if 
   next 
   '-- you chould select the first one
   if cboList.items.count > 0 then
      cboList.SelectedItem = cboList.item(0)
   end if 

   '-- continuation of hte default choice
   cboList.SelectedItem = defaultChoice

end sub
Maudite