简体   繁体   中英

What's the best way to create a drop-down list in a Windows application using Visual Basic?

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?

I'm using Visual Studio 2008.

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.

yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList

通过将DropDownStyle属性更改为“ DropDownList”,winforms中的组合框可以用作不可编辑的下拉列表:我认为没有单独的下拉列表控件。

Create two text boxes next to each other TextBox1 and TextBox2, for TextBox2 set multiple lines and autosize.

create your dropdown list somewhere. In my example it was in a different sheet with about 13,000 entries.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM