How should I fix this error: An unhandled exception of type 'System.InvalidCastExcept
How should I fix this error: An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
On this line:
Code:
If UCase$(Microsoft.VisualBasic.Left(lb.Items(i), StrLen)) = ItmStr Then
Here is the Code
Code:
Additional information: Cast from type 'DataRowView' to type 'String' is not valid.
]Private Sub SelectString(ByVal lb As ListBox, ByVal ItmStr As String)
Dim StrLen As Integer, i As Integer
StrLen = Len(ItmStr)
If StrLen = 0 Then Exit Sub
ItmStr = UCase$(ItmStr)
For i = 0 To lb.Items.Count - 1
If UCase$(Microsoft.VisualBasic.Left(lb.Items(i), StrLen)) = ItmStr Then
lb.SelectedIndex = i
Exit For
End If
Next i
End Sub
Private Sub txtSearch_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearch.KeyPress
Static ItmStr As String
Dim c As String
If Asc(e.KeyChar) = Keys.Escape Then
ItmStr = ""
lstListMachines.SelectedIndex = -1
ElseIf Asc(e.KeyChar) = Keys.Back Then
If Len(ItmStr) > 0 Then
ItmStr = Microsoft.VisualBasic.Left(ItmStr, Len(ItmStr) - 1)
End If
SelectString(lstListMachines, ItmStr)
Else
c = e.KeyChar
If c >= " " And c <= "~" Then
ItmStr = ItmStr & e.KeyChar
SelectString(lstListMachines, ItmStr)
End If
End If
End Sub