如何检测listbox和combobox里面重复的值?

有时候需要检查待插入的值在listbox或者combobox里面有没有重复。可用两种方法:

一、循环法
'窗口中放入3个控件,list1,textbox1,command1测试OK!
Private Sub Command1_Click()
Addstr Text1.Text
End Sub
Sub Addstr(ByVal Str As String)
        Dim i As Integer
        For i = 0 To List1.ListCount - 1
            If Str = List1.List(i) Then MsgBox "已经存在": Exit Sub
        Next
        List1.AddItem Str
End Sub

二、sendmessage法(比较快)
n=SendMessage(List1.hwnd, LB_FindStringExact, -1, ByVal "重复")
If  n>-1 Then
  MsgBox "已存在!"
End If

 

你可能感兴趣的:(list,command,textbox,integer,string,测试)