组合框搜索 无效参数=值 '0' 对"索引"无效。参数名称索引 vb.net



我使用文本框在访问数据库中搜索,然后在组合框中显示匹配项,它运行良好,但当用户输入不匹配的条目时会出现错误

这是我的代码:

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="0"的值对于"index"无效。参数名称索引vb.net

问题是如何处理这个错误

在将数据源绑定到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   

最新更新