0

我正在使用文本框在访问数据库中搜索,然后在组合框中显示匹配项,它工作正常,但是当用户输入不匹配的条目时会产生错误,

这是我的代码:

Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
    Try
        ComboBox3.DataSource = Me.MyDataset.Items.Select("ItemName like '%" & TextBox9.Text & "%'")


        ComboBox3.Update()

        If ComboBox3.Items.Count <> 0 Then

            ComboBox3.DroppedDown = True
            Me.Cursor = Cursors.Default
            Cursor.Show()
        Else

            If ComboBox3.DroppedDown = True Then
                ComboBox3.DroppedDown = False
            End If

        End If

        If TextBox9.Text = "" Then
            ComboBox3.DroppedDown = False
        End If
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
    End Try
End Sub   

当用户输入错误或不匹配时,会出现此错误:invalidargument=value of '0' is not valid for 'index'。参数名称索引 vb.net

问题是如何处理这个错误

4

1 回答 1

0

您应该在将它们绑定到 ComboBox 之前检查您的数据源是否包含项目。

我不知道什么是类型MyDataset,但是Option Infer On应该可以使用以下内容,否则您应该更改该Dim dataSource =行以包含该类型。

    Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
        Try

            Dim dataSource = Me.MyDataset.Items.Select("ItemName like '%" & TextBox9.Text & "%'")

            If dataSource.Items.Count > 0 Then
                ComboBox3.DataSource = datasource
                ComboBox3.Update()
            End If

            If ComboBox3.Items.Count <> 0 Then
                ComboBox3.DroppedDown = True
                Me.Cursor = Cursors.Default
                Cursor.Show()
            Else
                If ComboBox3.DroppedDown = True Then
                    ComboBox3.DroppedDown = False
                End If
            End If

            If TextBox9.Text = "" Then
                ComboBox3.DroppedDown = False
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
        End Try
    End Sub   
于 2013-08-11T10:56:58.913 回答