listview:lvData
acctcode first last
1 DFNTN High Definition
2 BLNCE World Balance
3 RNBOW Rain Bow
我想使用lvData
在textbox
中添加数据。但首先程序必须检查textbox
中提供的数据是否已存在。基于acctcode
列。
这是我的代码:
Dim articlecheck As String = TEXTBOX.text
Dim founditem As ListViewItem = LVDATA.FindItemWithText(articlecheck)
If Not (founditem Is Nothing) Then
MessageBox.Show("Data already exists!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
Else
'ADD DATA in the LISTVIEW'
问题是如果我在文本框中放置了类似这样的内容:DFN
,BLN
,RNB
。我的程序仍会显示"Data alrleady exist!"
答案 0 :(得分:2)
使用这些参数尝试相同的FindItemWithText方法。
LVDATA.FindItemWithText(articlecheck,False,0,False)
答案 1 :(得分:1)
你必须循环子项,因为FindItemWithText
将搜索所有子项目中的文字:
有代码:
Private Function FindSubItem(ByVal lv As ListView, ByVal SearchString As String) As Boolean
'find column index in listview by name "acctcode"
Dim idx = (From c In ListView1.Columns Where c.Text = "acctcode" Select c = c.Index).First()
For Each itm As ListViewItem In lv.Items
'search only subitems of column "acctcode"
If itm.SubItems(idx).Text = SearchString Then Return True
Next
Return False
End Function
然后使用:
Dim articlecheck As String = TEXTBOX.text
If FindSubItem(LVDATA, articlecheck) = True Then
MessageBox.Show("Data already exists!", "Duplicate", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
Else
'add Your item
End If