Results 1 to 14 of 14

Thread: [RESOLVED] Listview Search as type & show in Richtextbox error

  1. #1
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Resolved [RESOLVED] Listview Search as type & show in Richtextbox error

    Hi,

    im encountering error in this code, Kindly help me


    Code:
    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
            ' Dim selArray As ListView.SelectedListViewItemCollection = ListView1.SelectedItems
            '  If selArray.Count > 0 Then
            ListView1.Items.Clear()
            Dim itm As ListViewItem
            Dim i As Integer
    
            For i = 0 To ListView1.Items.Count - 1
                ListView1.Items.Item(i).BackColor = Color.White
                ListView1.Items.Item(i).ForeColor = Color.Black
            Next
    
            With ListView1
                itm = .FindItemWithText(txtSearch.Text, True, 0, True)
                'tell me what im going to do in this line for searching that kind of method
    
                rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm(0).Text))
    
                If Not itm Is Nothing Then
    
                    .Items.Item(itm.Index).EnsureVisible()
                    .Items.Item(itm.Index).BackColor = Color.CornflowerBlue
                    .Items.Item(itm.Index).ForeColor = Color.White
                    .Items.Item(itm.Index).Selected = True
    
    
                Else
                    MsgBox("No Record Found!")
                    For i = 0 To ListView1.Items.Count - 1
                        ListView1.Items.Item(i).BackColor = Color.White
                        ListView1.Items.Item(i).ForeColor = Color.Black
                    Next
                    .Items(0).EnsureVisible()
                    .Items.Item(0).BackColor = Color.CornflowerBlue
                    .Items.Item(0).ForeColor = Color.White
                    .Items.Item(0).Selected = True
    
                    txtSearch.SelectionStart = 0
                    txtSearch.SelectionLength = Len(txtSearch.Text)
                    txtSearch.Focus()
                End If
                '   End if
            End With
            itm = Nothing
    
        End Sub
    Error Line.
    Code:
    rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm(0).Text))

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,859

    Re: Listview Search as type & show in Richtextbox error

    What is the error message? What are the values of the data you're using?

  3. #3
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    this is the error msg:-

    Error 1 Class 'System.Windows.Forms.ListViewItem' cannot be indexed because it has no default property. C:\Users\KMV\Desktop\Code Library\Projects\WindowsApplication1\WindowsApplication1\Form1.vb 61 80 WindowsApplication1

  4. #4
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,257

    Re: Listview Search as type & show in Richtextbox error

    itm(0).Text is wrong, the correct is itm.Text
    Also, you should add
    vb Code:
    1. rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm(0).Text))

    after
    vb Code:
    1. If Not itm Is Nothing Then
    just in case .FindItemWithText(txtSearch.Text, True, 0, True) returns nothing

    Try this new one
    vb Code:
    1. Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    2.         ' Dim selArray As ListView.SelectedListViewItemCollection = ListView1.SelectedItems
    3.         '  If selArray.Count > 0 Then
    4.         ListView1.Items.Clear()
    5.         Dim itm As ListViewItem
    6.         Dim i As Integer
    7.  
    8.         For i = 0 To ListView1.Items.Count - 1
    9.             ListView1.Items.Item(i).BackColor = Color.White
    10.             ListView1.Items.Item(i).ForeColor = Color.Black
    11.         Next
    12.  
    13.         With ListView1
    14.             itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    15.             'tell me what im going to do in this line for searching that kind of method
    16.  
    17.  
    18.             If itm IsNot Nothing Then
    19.  
    20.                 rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
    21.                 .Items.Item(itm.Index).EnsureVisible()
    22.                 .Items.Item(itm.Index).BackColor = Color.CornflowerBlue
    23.                 .Items.Item(itm.Index).ForeColor = Color.White
    24.                 .Items.Item(itm.Index).Selected = True
    25.  
    26.  
    27.             Else
    28.                 MsgBox("No Record Found!")
    29.                 For i = 0 To ListView1.Items.Count - 1
    30.                     ListView1.Items.Item(i).BackColor = Color.White
    31.                     ListView1.Items.Item(i).ForeColor = Color.Black
    32.                 Next
    33.                 .Items(0).EnsureVisible()
    34.                 .Items.Item(0).BackColor = Color.CornflowerBlue
    35.                 .Items.Item(0).ForeColor = Color.White
    36.                 .Items.Item(0).Selected = True
    37.  
    38.                 txtSearch.SelectionStart = 0
    39.                 txtSearch.SelectionLength = Len(txtSearch.Text)
    40.                 txtSearch.Focus()
    41.             End If
    42.             '   End if
    43.         End With
    44.         itm = Nothing
    45.  
    46.     End Sub

  5. #5
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    Hi 4x2y
    thanks in advance
    its giving error as below and highlighting the code

    InvalidArgument=Value of '0' is not valid for 'startIndex'.
    Parameter name: startIndex

    Code:
    itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    if i use
    Code:
    Rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
    error as below

    InvalidArgument=Value of '0' is not valid for 'startIndex'.
    Parameter name: startIndex

  6. #6
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,257

    Re: Listview Search as type & show in Richtextbox error

    That's mean the listview is empty!

    Why you clear it at the beginning ListView1.Items.Clear()

  7. #7
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    Ops sorry it is working
    ListView1.Items.Clear() is the one giving error

    its not refreshing the listview when i clear the textbox.

  8. #8
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    Quote Originally Posted by 4x2y View Post
    That's mean the listview is empty!

    Why you clear it at the beginning ListView1.Items.Clear()
    i did not notice that. sorry. how do we can refrsh the listview n empty the rtb1 if the textsearch is empty?

  9. #9
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,257

    Re: Listview Search as type & show in Richtextbox error

    Try this new one
    vb Code:
    1. Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    2.  
    3.         If txtSearch.text = String.Empty Then
    4.             rtb1.Text = String.Empty
    5.             If ListView1.SelectedItems.Count > 0 Then
    6.                 ListView1.SelectedItems(0).Selected = False
    7.             End If
    8.  
    9.         Else
    10.  
    11.  
    12.             Dim itm As ListViewItem
    13.             Dim i As Integer
    14.  
    15.             For i = 0 To ListView1.Items.Count - 1
    16.                 ListView1.Items.Item(i).BackColor = Color.White
    17.                 ListView1.Items.Item(i).ForeColor = Color.Black
    18.             Next
    19.  
    20.             With ListView1
    21.                 itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    22.                 'tell me what im going to do in this line for searching that kind of method
    23.  
    24.  
    25.                 If itm IsNot Nothing Then
    26.  
    27.                     rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
    28.                     .Items.Item(itm.Index).EnsureVisible()
    29.                     .Items.Item(itm.Index).BackColor = Color.CornflowerBlue
    30.                     .Items.Item(itm.Index).ForeColor = Color.White
    31.                     .Items.Item(itm.Index).Selected = True
    32.  
    33.  
    34.                 Else
    35.                     MsgBox("No Record Found!")
    36.                     For i = 0 To ListView1.Items.Count - 1
    37.                         ListView1.Items.Item(i).BackColor = Color.White
    38.                         ListView1.Items.Item(i).ForeColor = Color.Black
    39.                     Next
    40.                     .Items(0).EnsureVisible()
    41.                     .Items.Item(0).BackColor = Color.CornflowerBlue
    42.                     .Items.Item(0).ForeColor = Color.White
    43.                     .Items.Item(0).Selected = True
    44.  
    45.                     txtSearch.SelectionStart = 0
    46.                     txtSearch.SelectionLength = Len(txtSearch.Text)
    47.                     txtSearch.Focus()
    48.                 End If
    49.                 '   End if
    50.             End With
    51.             itm = Nothing
    52.         End If
    53.  
    54.     End Sub

  10. #10
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    No luck
    its clearing the Rtb1
    not refreshing listview
    listview item still highlited

  11. #11
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,257

    Re: Listview Search as type & show in Richtextbox error

    Hope the following works
    vb Code:
    1. Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    2.  
    3.         Dim itm As ListViewItem
    4.         Dim i As Integer
    5.  
    6.  
    7.  
    8.         If txtSearch.Text = String.Empty Then
    9.  
    10.             For i = 0 To ListView1.Items.Count - 1
    11.                 ListView1.Items.Item(i).BackColor = Color.White
    12.                 ListView1.Items.Item(i).ForeColor = Color.Black
    13.             Next
    14.  
    15.         Else
    16.  
    17.  
    18.             With ListView1
    19.                 itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    20.  
    21.                 If itm IsNot Nothing Then
    22.  
    23.                     rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
    24.                     .Items.Item(itm.Index).EnsureVisible()
    25.                     .Items.Item(itm.Index).BackColor = Color.CornflowerBlue
    26.                     .Items.Item(itm.Index).ForeColor = Color.White
    27.                     .Items.Item(itm.Index).Selected = True
    28.  
    29.  
    30.                 Else
    31.                     MsgBox("No Record Found!")
    32.                     For i = 0 To ListView1.Items.Count - 1
    33.                         ListView1.Items.Item(i).BackColor = Color.White
    34.                         ListView1.Items.Item(i).ForeColor = Color.Black
    35.                     Next
    36.  
    37.                     .Items(0).EnsureVisible()
    38.                     .Items.Item(0).BackColor = Color.CornflowerBlue
    39.                     .Items.Item(0).ForeColor = Color.White
    40.                     .Items.Item(0).Selected = True
    41.  
    42.                     txtSearch.SelectionStart = 0
    43.                     txtSearch.SelectionLength = Len(txtSearch.Text)
    44.                     txtSearch.Focus()
    45.                 End If
    46.  
    47.             End With
    48.  
    49.         End If
    50.  
    51.     End Sub

  12. #12
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    its working but with small error

    its highlighting 2 items

  13. #13
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,257

    Re: Listview Search as type & show in Richtextbox error

    Another one
    vb Code:
    1. Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
    2.  
    3.         Dim itm As ListViewItem
    4.  
    5.         If txtSearch.Text = String.Empty Then
    6.  
    7.             RemoveHighlight()
    8.  
    9.         Else
    10.  
    11.  
    12.             With ListView1
    13.                 itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    14.  
    15.                 If itm IsNot Nothing Then
    16.                     RemoveHighlight()
    17.                     rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
    18.                     .Items.Item(itm.Index).EnsureVisible()
    19.                     .Items.Item(itm.Index).BackColor = Color.CornflowerBlue
    20.                     .Items.Item(itm.Index).ForeColor = Color.White
    21.                     .Items.Item(itm.Index).Selected = True
    22.  
    23.  
    24.                 Else
    25.                     MsgBox("No Record Found!")
    26.                     RemoveHighlight()
    27.  
    28.                     .Items(0).EnsureVisible()
    29.                     '.Items.Item(0).BackColor = Color.CornflowerBlue
    30.                     '.Items.Item(0).ForeColor = Color.White
    31.                     .Items.Item(0).Selected = True
    32.  
    33.                     txtSearch.SelectionStart = 0
    34.                     txtSearch.SelectionLength = Len(txtSearch.Text)
    35.                     txtSearch.Focus()
    36.                 End If
    37.  
    38.             End With
    39.  
    40.         End If
    41.  
    42.     End Sub
    43.  
    44.     Private Sub RemoveHighlight()
    45.         For i As Integer = 0 To ListView1.Items.Count - 1
    46.             ListView1.Items.Item(i).BackColor = Color.White
    47.             ListView1.Items.Item(i).ForeColor = Color.Black
    48.         Next
    49.     End Sub

  14. #14
    Junior Member
    Join Date
    Dec 06
    Posts
    28

    Re: Listview Search as type & show in Richtextbox error

    thanks alot 4x2y
    its working find.
    u solved the issue n save my day.
    thank u very very much

Posting Permissions

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