2008-08-18 56 views
5

在Windows窗体中实现多选选项的最佳方式是什么?我想从列表中强制执行一个选择,从默认值开始。WinForms上的多项选择

它看起来像一个组合框将是一个不错的选择,但有没有办法指定一个非空白的默认值?
我可以在代码中将它设置在适当的初始化点,但我觉得我错过了一些东西。

回答

8

如果您只想从组中选择一个答案,那么RadioButton控件将是您最合适的,或者如果您有很多选项,则可以使用ComboBox。要设置默认值,只需将该项目添加到ComboBox的集合中,并将SelectedIndex或SelectedItem设置为该项目。

取决于你有多少选择看,你可以使用一个列表框与SelectionMode属性设置为MultiSimple,如果这将是多项选择,或者你可以使用CheckBox控件。

2

您可以使用组合框,将DropDownStyle属性设置为DropDownList,将SelectedIndex设置为0(或任何默认项目)。这将强制始终从列表中选择一个项目。如果你忘记这样做,用户可以直接输入别的东西在编辑框中的部分 - 如果你是给用户一个选择小单子,然后用单选按钮坚持这将是坏:)

1

。但是,如果您希望使用动态或长列表的组合框。将样式设置为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