在我的应用程序中,我想在列表视图中添加一个包含3个子项的项目
第一个子项是:项目的索引号
第二个子项是:项目的描述
最后一个子项是:目录路径
实施例:
在添加新项目之前,我尝试搜索listview是否已包含第三个子项目(目录路径),其中包含我已经创建的函数:
' 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
...现在,问题是例如,如果我首先在listview中添加一个包含目录“C:\ electro”的项目作为第三个子项目,如图中所示,那么后来我无法添加一个带有“C:\”目录的新项目,因为我的函数没有搜索全文,当我搜索“C”时,我的函数找到“C:\ Electro” :\“,它搜索一段文字,我需要另外的文字。
然后我需要改进功能,在listview中搜索全文,而不是一段文字。
最后一个例子:
如果我在listview中有一个带有字符串“C:\ Electro”的项目,并且我在listviews项目中搜索是否存在“C:\”,则所需的结果为“FALSE”(不存在目录C:\ ,是C:\ Electro)
更新:
从类中提取的代码示例,如果你想看到我的意思......
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
答案 0 :(得分:1)
这不是一个正确的答案,但你可以尝试一个技巧:
再添加一列(隐藏)
并写入"^" + Directory Path + "^"
,然后搜索^C:\^
,它只会找到C:\
而不是C:\Electro
答案 1 :(得分:1)
这是一个简单的小函数,它接受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
你会这样称呼:
If Not FindItem(ListView_Sendto, 2, TextBox_Sendto_Directory.Text) Then
比较不区分大小写。如果您想要区分大小写,可以使用CompareTo方法而不是相等。
如果有必要,可以在列索引超出范围时添加错误捕获。
答案 2 :(得分:0)
最后......我做了片段:
#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