VB.net ComboBox auto drop down on input

自作多情 提交于 2019-12-18 09:18:58

问题


I want to create a ComboBox that automaitcally drops down the words containing the letter based on the input. My dropdownstyle is dropdown so the combobox has an input field.

For example i would input the letter A or a I want the ComboBox to automatically dropdown the words which contains the letter A or a. The contents of the ComboBox are being set by myself manually.

Is this possible? Thanks in advance.


回答1:


You have to set these

AutoCompleteMode: SuggestAppend
AutoCompleteSource: ListItems
DropDownStyle: DropDown

Suppose your combo has these items then you have to add them to the autocompletecustomsource also

ComboBox1.Items.Add("10")
ComboBox1.Items.Add("92")
ComboBox1.Items.Add("9000")
ComboBox1.Items.Add("9001")

ComboBox1.AutoCompleteCustomSource.Add("10")
ComboBox1.AutoCompleteCustomSource.Add("92")
ComboBox1.AutoCompleteCustomSource.Add("9000")
ComboBox1.AutoCompleteCustomSource.Add("9001")

ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource



回答2:


Yes, this is possible via the AutoCompleteMode and AutoComplete, like this:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
        ComboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

        'Add some options
        ComboBox1.AutoCompleteCustomSource.Add("ABC")
        ComboBox1.AutoCompleteCustomSource.Add("BCD")
        ComboBox1.AutoCompleteCustomSource.Add("CDE")
    End Sub

    'Add ComboBox1.Text to AutoCompleteCustomSource collection when leaving ComboBox
    Private Sub ComboBox1_Leave(ByVal sender As System.Object, _
               ByVal e As System.EventArgs) Handles ComboBox1.Leave
        ComboBox1.AutoCompleteCustomSource.Add(ComboBox1.Text)
    End Sub
End Class

Read AutoCompleteMode Enumeration for more information.

Read AutoCompleteSource Enumeration for more information.



来源:https://stackoverflow.com/questions/18670245/vb-net-combobox-auto-drop-down-on-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!