how would i go about.. if text1 is in list1, don't add
:\ If it's not, add!
help please
Printable View
how would i go about.. if text1 is in list1, don't add
:\ If it's not, add!
help please
You have 2 basic choices:
1. Loop thru the list items from zero to .Listcount-1 and compare each .List(n) with the textbox text
2. Use API and quickly determine if match can be found. Search forum for LB_FINDSTRINGEXACT for an example.
i'm not quite sure if i should be using list2.listindex or not. but yeah :\ help fix that?Code:If text1.text = List2.ListIndex Then
Exit Sub
Else: List2.AddItem text1.text
End If
Code:Dim lngIndex As Long
Dim bFound As Boolean
For lngIndex = 0 to List2.Listcount -1
If Text1.Text = List2.List(lngIndex) Then
bFound = True
Exit For
End If
Next
If not bFound Then
List2.AddItem Text1.Text
Else
MsgBox Text1.Text & "already added"
End If
thanks worked like a charm :)
I'm glad that works for you but as LaVolpe suggested this is the faster way.
Option Explicit
Code: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 SomeSub()
If SendMessage(List2.hwnd, LB_FINDSTRINGEXACT, -1&, _
ByVal Text1.Text) > -1& Then
MsgBox Text1.Text & " already added"
Else
List2.AddItem Text1.Text
End If
End Sub