[英]Find exact text inside Listview subitems?
In my application I want to add a item of 3 subitems in a listview 在我的应用程序中,我想在列表视图中添加3个子项
first subitem is : the index number of the item 第一个子项目是:该项目的索引号
second subitem is : A description for the item 第二个子项目是:该项目的说明
last subitem is : A directory path 最后一个子项是:目录路径
Example: 例:
Just before adding a new item I try to search if the listview already contains the third subitem (the directory path) with a function I've made: 在添加新项目之前,我尝试搜索列表视图是否已经包含具有我已创建的功能的第三个子项目(目录路径):
' Find ListView Text
Private Function Find_ListView_Text(ByVal ListView As ListView, ByVal Text As String) As Boolean
Try : Return Convert.ToBoolean(ListView.FindItemWithText(Text)) : Catch : Return True : End Try
End Function
...Now, the problem is for example if I first add in the listview a Item wich contains the directory "C:\\electro" as the third subitem like seen in the image, then later I can't add a new item with a "C:\\" directory because my function is not searching for a full text, my function founds "C:\\Electro" when I search for "C:\\", it searchs for a piece of text and I need the otherwise. ...现在,问题是,例如,如果我首先在列表视图中添加一个包含目录“ C:\\ electro”的项目作为图像中的第三个子项目,那么以后我将无法添加带有“ C:\\”目录,因为我的函数未在搜索全文,当我搜索“ C:\\”时,我的函数会找到“ C:\\ Electro”,它会搜索一段文本,否则我需要。
Then I need to improve the function to search inside a listview for a full text, not for a piece of text. 然后,我需要改进在列表视图内搜索全文而不是文本的功能。
Last example: 最后一个例子:
If I have an item in the listview with the string "C:\\Electro" and I search if exist "C:\\" in the listviews items, the desired result is "FALSE" (Not exists the directory C:\\, is C:\\Electro) 如果我在列表视图中有一个字符串为“ C:\\ Electro”的项目,并且搜索列表视图中的项目是否存在“ C:\\”,则所需结果为“ FALSE”(目录C:\\不存在,为C :\\电)
UPDATE:
更新:
A code example extracted from the class, if you want to see what I mean... 如果您想了解我的意思,请从该类中提取一个代码示例...
Private Sub TextBoxes_Sendto_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox_Sendto_Directory.TextChanged, _
TextBox_Sendto_Description.TextChanged
If TextBox_Sendto_Description.TextLength <> 0 _
AndAlso TextBox_Sendto_Directory.TextLength <> 0 Then
If Not Find_ListView_Text(ListView_Sendto, TextBox_Sendto_Directory.Text) Then
Label_Sendto_Status.Text = "Directory ready to add"
Label_Sendto_Status.ForeColor = Color.YellowGreen
Button_Sendto_Add.Enabled = True
Else
Label_Sendto_Status.Text = "Directory already added"
Label_Sendto_Status.ForeColor = Color.Red
Button_Sendto_Add.Enabled = False
End If
Else
Button_Sendto_Add.Enabled = False
End If
End Sub
Private Sub Button_Sendto_Add_Click(sender As Object, e As EventArgs) Handles Button_Sendto_Add.Click
Dim item = ListView_Sendto.AddItem(ListView_Sendto.Items.Count + 1)
item.SubItems.Add(TextBox_Sendto_Description.Text)
item.SubItems.Add(TextBox_Sendto_Directory.Text)
End Sub
' Find ListView Text
Private Function Find_ListView_Text(ByVal ListView As ListView, ByVal Text As String) As Boolean
Try : Return Convert.ToBoolean(ListView.FindItemWithText(Text)) : Catch : Return True : End Try
End Function
This is not a proper answer but one trick you can try : 这不是一个正确的答案,但是您可以尝试以下一种技巧:
just add one more column(hidden) 只需再添加一列(隐藏)
and write in that "^" + Directory Path + "^"
and then search for ^C:\\^
, it will only find C:\\
not C:\\Electro
并输入
"^" + Directory Path + "^"
,然后搜索^C:\\^
,它将只找到C:\\
而不是C:\\Electro
Here's a simple little function that takes the ListViewItemcollection, the zero-based column index and a search string and returns whether the search string exists in any subitem in that column: 这是一个简单的小函数,它接受ListViewItemcollection,从零开始的列索引和搜索字符串,并返回该字符串中任何子项目中是否存在搜索字符串:
Private Function FindItem(ItemList As ListView.ListViewItemCollection, ColumnIndex As Integer, SearchString As String) As Boolean
For Each Item As ListViewItem In ItemList
If Item.SubItems(ColumnIndex).Text = SearchString Then
Return True
End If
Next
Return False
End Function
You would call it like this: 您可以这样称呼它:
If Not FindItem(ListView_Sendto, 2, TextBox_Sendto_Directory.Text) Then
The comparison is case insensitive. 比较不区分大小写。 If you want case sensitivity you could use the CompareTo method instead of equality.
如果要区分大小写,可以使用CompareTo方法而不是相等方法。
If necessary you could add error trapping in case the column index is out of range. 如有必要,您可以添加错误捕获,以防列索引超出范围。
Finally... I did the snippet: 终于...我做了摘录:
#Region " [ListView] Find ListView Text "
' [ListView] Find ListView Text Function
'
' // By Elektro H@cker
'
' Examples :
' MsgBox(Find_ListView_Text(ListView1, "Test"))
' MsgBox(Find_ListView_Text(ListView1, "Test", 2, True, True))
' If Find_ListView_Text(ListView1, "Test") Then...
Private Function Find_ListView_Text(ByVal ListView As ListView, _
ByVal SearchString As String, _
Optional ByVal ColumnIndex As Int32 = Nothing, _
Optional ByVal MatchFullText As Boolean = True, _
Optional ByVal IgnoreCase As Boolean = True) As Boolean
Dim ListViewColumnIndex As Int32 = ListView.Columns.Count - 1
Select Case ColumnIndex
Case Is < 0, Is > ListViewColumnIndex ' ColumnIndex is out of range
Throw New Exception("ColumnIndex is out of range. " & vbNewLine & _
"ColumnIndex Argument: " & ColumnIndex & vbNewLine & _
"ColumnIndex ListView: " & ListViewColumnIndex)
Case Nothing ' ColumnIndex is nothing
If MatchFullText AndAlso IgnoreCase Then ' Match full text, All columns, IgnoreCase
For Each Item As ListViewItem In ListView.Items
For X As Int32 = 0 To ListViewColumnIndex
If Item.SubItems(X).Text.ToLower = SearchString.ToLower Then Return True
Next
Next
ElseIf MatchFullText AndAlso Not IgnoreCase Then ' Match full text, All columns, CaseSensitive
For Each Item As ListViewItem In ListView.Items
For X As Int32 = 0 To ListViewColumnIndex
If Item.SubItems(X).Text = SearchString Then Return True
Next
Next
ElseIf Not MatchFullText AndAlso IgnoreCase Then ' Match part of text, All columns, IgnoreCase
If ListView1.FindItemWithText(SearchString) IsNot Nothing Then _
Return True _
Else Return False
ElseIf Not MatchFullText AndAlso Not IgnoreCase Then ' Match part of text, All columns, CaseSensitive
For Each Item As ListViewItem In ListView.Items
For X As Int32 = 0 To ListViewColumnIndex
If Item.SubItems(X).Text.Contains(SearchString) Then Return True
Next
Next
End If
Case Else ' ColumnIndex is other else
If MatchFullText AndAlso IgnoreCase Then ' Match full text, ColumnIndex, IgnoreCase
For Each Item As ListViewItem In ListView.Items
If Item.SubItems(ColumnIndex).Text.ToLower = SearchString.ToLower Then Return True
Next
ElseIf MatchFullText AndAlso Not IgnoreCase Then ' Match full text, ColumnIndex, CaseSensitive
For Each Item As ListViewItem In ListView.Items
If Item.SubItems(ColumnIndex).Text = SearchString Then Return True
Next
ElseIf Not MatchFullText AndAlso IgnoreCase Then ' Match part of text, ColumnIndex, IgnoreCase
For Each Item As ListViewItem In ListView.Items
If Item.SubItems(ColumnIndex).Text.ToLower.Contains(SearchString.ToLower) Then Return True
Next
ElseIf Not MatchFullText AndAlso Not IgnoreCase Then ' Match part of text, ColumnIndex, CaseSensitive
For Each Item As ListViewItem In ListView.Items
If Item.SubItems(ColumnIndex).Text.Contains(SearchString) Then Return True
Next
End If
End Select
Return False
End Function
#End Region
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.