我正在使用文本框在访问数据库中搜索,然后在组合框中显示匹配项,它工作正常但是当用户输入不匹配的条目时会产生错误,
这是我的代码:
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
问题是如何处理此错误
答案 0 :(得分: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