【发布时间】:2013-06-28 17:47:22
【问题描述】:
在我的应用程序中,我想在列表视图中添加一个包含 3 个子项的项目
第一个子项是:该项的索引号
第二个子项目是:项目的描述
最后一个子项是:目录路径
示例:
就在添加新项目之前,我尝试搜索列表视图是否已经包含第三个子项目(目录路径)以及我创建的函数:
' 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
...现在,问题是,例如,如果我首先在列表视图中添加一个项目,该项目包含目录 "C:\electro" 作为第三个子项,如图所示,然后稍后我无法使用 "C:\" 目录添加新项目,因为我的函数没有搜索全文,当我搜索“C”时,我的函数找到“C:\Electro” :\",它会搜索一段文本,我需要其他的。
然后我需要改进在列表视图中搜索全文而不是一段文本的功能。
最后一个例子:
如果我在列表视图中有一个带有字符串“C:\Electro”的项目,并且我在列表视图项目中搜索是否存在“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
【问题讨论】: