Results 1 to 6 of 6

Thread: A combobox limited to array

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309

    Post

    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

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Try this:
    Code:
    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
    [email protected]
    [email protected]
    ICQ#: 51055819


  3. #3
    Member
    Join Date
    Jan 1999
    Location
    Garden Grove, CA, Orange
    Posts
    55

    Post

    Set ComboBox style to 2-Dropdown List, and add all array items to comboBox.

    Code:
    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

  4. #4
    Lively Member
    Join Date
    Dec 1999
    Posts
    106

    Post

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 1999
    Posts
    309

    Post

    Thanx Serge,

    Works like a charm...

  6. #6
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    Post

    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)
    [email protected]



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width