将组合框'Droppeddown'值设置为false

时间:2016-12-14 17:14:32

标签: vb.net visual-studio

我有一个用于搜索数据库的组合框。下拉列表将显示用户正在搜索的内容是否具有匹配的项目,如果不是,则会缩回。但是,当我的代码将DroppedDown值更改为False时,我立即得到“ArgumentOutOfRangeException。”

错误还包括“附加信息:InvalidArgument ='0'的值对'index'无效。”

我不确定是怎么回事,因为这是我第一次尝试使用组合框并自动更新它们。

'Search box - Text Update Event
    Private Sub searchBoxCB_TextUpdate(sender As Object, e As EventArgs) Handles searchBoxCB.TextUpdate
        'Begin search when user types

        searchBoxCB.Items.Clear()

        'Check if search has numbers in it
        Dim letters() As Char = searchBoxCB.Text
        Dim noNumbers As Boolean = True

        For Each c As Char In letters

            If (IsNumeric(c)) Then

                noNumbers = False

            End If

        Next

        'Only search if no numbers
        If (noNumbers) Then

            searchDataPerson(searchBoxCB.Text, "Person1")
            searchDataPerson(searchBoxCB.Text, "Person2")

        End If

        searchBoxCB.SelectionStart = searchBoxCB.Text.Length + 1 'Move cursor to end

        Cursor.Current = Cursors.Default

    End Sub

    'Search PERSON operation
    Private Sub searchDataPerson(input As String, person As String)

        Dim itemAdded As Boolean = False

        myConnection.Open()

        Dim str As String = "SELECT * FROM AllEmployeeDirectory WHERE " + person + " Like """ + input + "%""" 'Searches for NAMES beginning with input
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection) '*
        dr = cmd.ExecuteReader '*

        'Reads through all data
        While dr.Read

            searchBoxCB.DroppedDown = True
            searchBoxCB.Items.Add(dr(person) + " - " + dr("Desk"))
            itemAdded = True

        End While

        If (Not itemAdded) Then

            searchBoxCB.DroppedDown = False 'ERROR OCCURS HERE

        End If

        myConnection.Close()

    End Sub

2 个答案:

答案 0 :(得分:0)

尽管有许多变体,但我无法让您的代码正常工作。

但是,通过设置组合框以使用此代码自动完成,我得到了我认为您打算工作的内容:

Option Infer On

Public Class Form1

    Public Shared Function ACSC(s1 As String, s2 As String) As AutoCompleteStringCollection
        'TODO: substitute your query for obtaining the data here
        Dim a = {"A", "B", "AB", "CBA", "CB", "BC", "ABC"}

        Dim sc As New AutoCompleteStringCollection
        sc.AddRange(a.ToArray())

        Return sc

    End Function

    Private Sub searchBoxCB_TextChanged(sender As Object, e As EventArgs) Handles searchBoxCB.TextChanged
        Dim txt = DirectCast(sender, ComboBox).Text
        searchBoxCB.AutoCompleteSource = AutoCompleteSource.CustomSource
        searchBoxCB.AutoCompleteCustomSource = ACSC(txt, "")

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        searchBoxCB.AutoCompleteMode = AutoCompleteMode.SuggestAppend

    End Sub

End Class

.ToArray()上的a以防a恰好是LINQ方法。)

信用:how List become AutoCompleteStringCollection

答案 1 :(得分:0)

经过一些令人沮丧的实验后,我开始实现对原始代码的简单解决方案。而不是searchBoxCB.DroppedDown = False我将其更改为searchBoxCB.DropDownHeight = 1,这实际上做了同样的事情。然后我还将高度重置回65,其中DroppedDown值设置为true。它现在按预期工作,不敢相信我之前没想过这个解决方法。