visual-studio 使用 Visual Basic 在 Windows 应用程序中创建下拉列表的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/102240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to create a drop-down list in a Windows application using Visual Basic?
提问by Eric Ness
I'd like to add a drop-down list to a Windows application. It will have two choices, neither of which are editable. What's the best control to use? Is it a combo box with the editing property set to No?
我想向 Windows 应用程序添加一个下拉列表。它将有两个选择,均不可编辑。最好的控件是什么?它是一个编辑属性设置为否的组合框吗?
I'm using Visual Studio 2008.
我正在使用 Visual Studio 2008。
回答by Sam Erwin
I'd suggest taking a look at the Windows Vista User Experience Guide. It sounds like you might be better off with radio buttons, or, if it's an explicit on/off type of situation, using a check box. I think we'd really need more info, though.
我建议看看Windows Vista 用户体验指南。听起来您最好使用单选按钮,或者,如果是明确的开/关类型的情况,请使用复选框。不过,我认为我们确实需要更多信息。
回答by JP Richardson
yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList
yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList
回答by Nikki9696
Set the DropDownStyle property to DropDownList.
将 DropDownStyle 属性设置为 DropDownList。
See http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle(VS.80).aspx
请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle(VS.80).aspx
回答by dpen2000
The combobox in winforms doubles as a uneditable drop down list by changing the DropDownStyle property to "DropDownList": I don't think there is a separate drop down list control.
通过将 DropDownStyle 属性更改为“DropDownList”,winforms 中的组合框可以兼作不可编辑的下拉列表:我认为没有单独的下拉列表控件。
回答by Scroude
Create two text boxes next to each other TextBox1 and TextBox2, for TextBox2 set multiple lines and autosize.
创建两个彼此相邻的文本框 TextBox1 和 TextBox2,为 TextBox2 设置多行和自动调整大小。
create your dropdown list somewhere. In my example it was in a different sheet with about 13,000 entries.
在某处创建下拉列表。在我的示例中,它位于不同的工作表中,大约有 13,000 个条目。
Below are two functions, the first drives the input box, TextBox1. as you type, the 2nd box, TextBox2 shows the remaining valid choices. The second function, loads the first textbox if you click on a choice in the second box
下面是两个函数,第一个驱动输入框TextBox1。在您键入时,第二个框 TextBox2 显示剩余的有效选项。第二个函数,如果您单击第二个框中的选项,则加载第一个文本框
In the below, cell B3 on the current sheet had to be loaded with the typed/selected answer. In my application, I only wanted to search for uppercase characters hence the UCase usage. My list data was 13302 entries in column Z
在下面,当前工作表上的单元格 B3 必须加载输入/选择的答案。在我的应用程序中,我只想搜索大写字符,因此使用 UCase。我的列表数据是 Z 列中的 13302 个条目
Private Sub TextBox1_Keyup(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim curtext As String
Dim k, where As Integer
Dim tmp As String
Dim bigtmp As String
curtext = TextBox1.Text
curtext = UCase(curtext)
TextBox1.Text = curtext
Range("b3").Value = curtext
If Len(curtext) = 0 Then
TextBox2.Visible = False
Exit Sub
End If
TextBox2.Visible = True
Application.ScreenUpdating = False
For k = 2 To 13303 ' YOUR LIST ROWS
tmp = Sheets("General Lookup").Range("Z" & k).Value ' YOUR LIST RANGE
where = InStr(1, tmp, TextBox1.Text, 1)
If where = 1 Then
bigtmp = bigtmp & tmp & Chr(13)
End If
Next
TextBox2.Text = bigtmp
Application.ScreenUpdating = True
End Sub
Private Sub TextBox2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Len(TextBox2.SelText) > 0 Then
TextBox1.Text = TextBox2.SelText
Range("b3").Value = TextBox2.SelText
TextBox2.Visible = False
End If
End Sub

