Results 1 to 2 of 2

Thread: How should I fix this error: An unhandled exception of type 'System.InvalidCastExcept

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    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

  2. #2
    Member
    Join Date
    May 2001
    Posts
    39
    lb.Items(i) is a object of type DataRowView, and the first parameter of Left function must be a string, instead try to use lb.Items(i).Text or something like this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width