Click to See Complete Forum and Search --> : A combobox limited to array
Inhumanoid
Dec 13th, 1999, 10:30 PM
Let's say I have an array called sValid().
I want to make a combobox that the user can only type in string that are inside sValid()
If a user tries to type in a string that is not in the array nothing should happen....
Example:
User tries to type in "testing"
This is possible because the array contains a string "testing"
Now, when the user tries to type in "testings" the result in the combobox text should be "testing"
How do I do this
Serge
Dec 14th, 1999, 12:45 AM
Try this:
Option Explicit
Private sValid() As String
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim strText As String
Dim i As Integer
Dim bIsFound As Boolean
If KeyAscii = 8 Then Exit Sub
strText = Combo1.Text & Chr(KeyAscii)
For i = 0 To UBound(sValid)
If InStr(UCase(sValid(i)), UCase(strText)) Then
bIsFound = True
End If
Next
If Not bIsFound Then KeyAscii = 0
End Sub
Private Sub Form_Load()
ReDim Preserve sValid(2)
sValid(0) = "Setting"
sValid(1) = "Close"
sValid(2) = "Testing"
Combo1.Text = ""
End Sub
------------------
Serge
Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)
jpark
Dec 14th, 1999, 04:05 AM
Set ComboBox style to 2-Dropdown List, and add all array items to comboBox.
Private Sub Form_Load()
myArr(0) = "Setting"
myArr(1) = "Close"
myArr(2) = "Testing"
LoadCombo Combo1, myArr
End Sub
Private Sub LoadCombo(cb As ComboBox, arr As Variant)
Dim i As Integer
cb.Clear
For i = 0 To UBound(arr)
cb.AddItem arr(i)
Next
End Sub
Joon
hayessj
Dec 14th, 1999, 11:05 AM
You could change this style of the combo to "dropdown list" and the user will only be able to select what is in the list. I you really need to let them type stuff in then this should work,
Drop a combo onto a form and paste the following code
Option Explicit
Dim sArray(2) As String
Private Sub Combo1_Change()
Dim i As Integer
Dim sCompare As String
Dim bOk As Boolean
sCompare = Combo1.Text
For i = 0 To UBound(sArray)
If Len(sArray(i)) >= Len(sCompare) Then
If Left(sArray(i), Len(sCompare)) = sCompare Then
bOk = True
Exit For
End If
End If
Next
If Not bOk Then
If Len(Combo1.Text) > 1 Then
Combo1.Text = Left(Combo1.Text, Len(Combo1.Text) - 1)
Else
Combo1.Text = ""
End If
End If
Combo1.SelStart = Len(Combo1.Text)
End Sub
Private Sub Form_Load()
Dim i As Integer
sArray(0) = "one"
sArray(1) = "two"
sArray(2) = "three"
For i = 0 To 2
Combo1.AddItem sArray(i)
Next
End Sub
Inhumanoid
Dec 14th, 1999, 05:22 PM
Thanx Serge,
Works like a charm...
onerrorgoto
Dec 14th, 1999, 06:17 PM
Hey InHumanoid
Altough you have got code from Serge that works for you, you might wanna check out the combobox in the forms2 object library.
It has a property
objekt.MatchEntry [= fmMatchEntry] that checks if what the user types is in the list automatically.
Check out the help for more info :)
------------------
On Error Goto Bed :0)
anders@zsystemdesign.se
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.