[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))
Re: Listview Search as type & show in Richtextbox error
What is the error message? What are the values of the data you're using?
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
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:
rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm(0).Text))
after
vb Code:
If Not itm Is Nothing Then
just in case .FindItemWithText(txtSearch.Text, True, 0, True) returns nothing
Try this new one
vb 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
If itm IsNot Nothing Then
rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
.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
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
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()
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.
Re: Listview Search as type & show in Richtextbox error
Quote:
Originally Posted by
4x2y
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?
Re: Listview Search as type & show in Richtextbox error
Try this new one
vb Code:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
If txtSearch.text = String.Empty Then
rtb1.Text = String.Empty
If ListView1.SelectedItems.Count > 0 Then
ListView1.SelectedItems(0).Selected = False
End If
Else
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
If itm IsNot Nothing Then
rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
.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 If
End Sub
Re: Listview Search as type & show in Richtextbox error
No luck
its clearing the Rtb1
not refreshing listview
listview item still highlited
Re: Listview Search as type & show in Richtextbox error
Hope the following works
vb Code:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim itm As ListViewItem
Dim i As Integer
If txtSearch.Text = String.Empty Then
For i = 0 To ListView1.Items.Count - 1
ListView1.Items.Item(i).BackColor = Color.White
ListView1.Items.Item(i).ForeColor = Color.Black
Next
Else
With ListView1
itm = .FindItemWithText(txtSearch.Text, True, 0, True)
If itm IsNot Nothing Then
rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
.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 With
End If
End Sub
Re: Listview Search as type & show in Richtextbox error
its working but with small error
its highlighting 2 items :(
Re: Listview Search as type & show in Richtextbox error
Another one :)
vb Code:
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
Dim itm As ListViewItem
If txtSearch.Text = String.Empty Then
RemoveHighlight()
Else
With ListView1
itm = .FindItemWithText(txtSearch.Text, True, 0, True)
If itm IsNot Nothing Then
RemoveHighlight()
rtb1.Text = System.IO.File.ReadAllText(IO.Path.Combine(scriptPath, itm.Text))
.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!")
RemoveHighlight()
.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 With
End If
End Sub
Private Sub RemoveHighlight()
For i As Integer = 0 To ListView1.Items.Count - 1
ListView1.Items.Item(i).BackColor = Color.White
ListView1.Items.Item(i).ForeColor = Color.Black
Next
End Sub
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