vb中怎么可以让list中的数据不重复
展开全部
在additem之前,查询一下list中当前的item是否与当前内容相同,如果相同则不再添加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般是在添加的时候防止出现重复项,用以下代码
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Sub Command1_Click()
Dim ret As Long
ret = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal Text1.Text)
If ret = -1 Then
List1.AddItem Text1.Text
Else
List1.ListIndex = ret
End If
End Sub
Private Sub Form_Load()
List1.AddItem "aaa"
List1.AddItem "ccc"
List1.AddItem "bbb"
List1.AddItem "ddd"
List1.AddItem "eee"
End Sub
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_FINDSTRINGEXACT = &H1A2
Private Sub Command1_Click()
Dim ret As Long
ret = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal Text1.Text)
If ret = -1 Then
List1.AddItem Text1.Text
Else
List1.ListIndex = ret
End If
End Sub
Private Sub Form_Load()
List1.AddItem "aaa"
List1.AddItem "ccc"
List1.AddItem "bbb"
List1.AddItem "ddd"
List1.AddItem "eee"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
从list的下界往上循环比较,有重复就remove
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的意思是添加不重复数据吗?
设你要添加的数据为abc
abc = Text1.Text '输入文本变量
For i = 0 To List1.ListCount - 1
If abc = List1.List(i) Then
Exit For
End If
Next i
If i = List1.ListCount And abc <> "" Then
List1.AddItem abc
End If
设你要添加的数据为abc
abc = Text1.Text '输入文本变量
For i = 0 To List1.ListCount - 1
If abc = List1.List(i) Then
Exit For
End If
Next i
If i = List1.ListCount And abc <> "" Then
List1.AddItem abc
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是说每点一次铵钮,就会在后面添加内容吗?如果是这样在操作前前清空,list1.clear
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询