【发布时间】:2008-08-18 13:44:23
【问题描述】:
在 Windows 窗体中实现多项选择选项的最佳方式是什么?我想强制从列表中选择一个,从默认值开始。
ComboBox 似乎是一个不错的选择,但有没有办法指定非空白默认值?
我可以在代码中的某个适当的初始化点设置它,但我觉得我错过了一些东西。
【问题讨论】:
在 Windows 窗体中实现多项选择选项的最佳方式是什么?我想强制从列表中选择一个,从默认值开始。
ComboBox 似乎是一个不错的选择,但有没有办法指定非空白默认值?
我可以在代码中的某个适当的初始化点设置它,但我觉得我错过了一些东西。
【问题讨论】:
如果您只想从小组中获得一个答案,那么 RadioButton 控件将是您的最佳选择,或者如果您有很多选项,您可以使用 ComboBox。要设置默认值,只需将项目添加到 ComboBox 的集合中并将 SelectedIndex 或 SelectedItem 设置为该项目。
根据您查看的选项数量,您可以使用将 SelectionMode 属性设置为 MultiSimple 的 ListBox,如果它是多项选择,或者您可以使用 CheckBox 控件。
【讨论】:
您应该能够将 ComboBox.SelectedIndex 属性设置为您想要的默认值。
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx
【讨论】:
在插入项目后使用ComboBox.SelectedItem 或SelectedIndex 属性来选择默认项目。
您还可以考虑使用RadioButton 控件来强制选择单个选项。
【讨论】:
您可以使用将DropDownStyle 属性设置为DropDownList 并将SelectedIndex 设置为0(或任何默认项)的组合框。这将强制始终选择列表中的项目。如果您忘记这样做,用户可以在编辑框部分输入其他内容 - 这会很糟糕:)
【讨论】:
如果您要为用户提供一小部分选择,请坚持使用单选按钮。但是,如果您想将组合框用于动态列表或长列表。将样式设置为 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
【讨论】: