CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Bogotá, Colombia
    Posts
    37

    ComboBox text entry

    I am using the keypress routine in a control array of Combo boxes to match the entered text with similar entries in the database. It is like an MSAccess field that has a foreign key relation behaves when the user presses the key "R" and the foreign field has a field with the value "Radio;" in the combo box the "R" appears as entered text and "adio" appears as highlighted text that can change if the next key entered is not an "a."



    private Sub ComboRow_KeyPress(Index as Integer, KeyAscii as Integer)
    Dim CaseCount, ValidateIndex as Integer
    Dim Char, strQuery as string

    Call RowOptions(strQuery)
    Char = Chr(KeyAscii)

    for CaseCount = 0 to Index
    With ComboColumnaComparación
    Select Case Index

    Case 0
    ValidateIndex = 0
    Call ValidateEntry(Char, strQuery, _
    ValidateIndex, KeyAscii)

    Case 1
    ValidateIndex = 1
    Call ValidateEntry(Char, strQuery, _
    ValidateIndex, KeyAscii)

    Case 2
    ValidateIndex = 2
    Call ValidateEntry(Char, strQuery, _
    ValidateIndex, KeyAscii)

    Case 3
    ValidateIndex = 3
    Call ValidateEntry(Char, strQuery, _
    ValidateIndex, KeyAscii)

    End Select
    End With
    next
    End Sub

    public Sub ValidateEntry(byval Char, byval strQuery, _
    byval ValidateIndex, cKeyAscii)
    Dim Comparison as string
    Dim Length as Integer

    If len(ComboRow(ValidateIndex).Text) = 0 then
    Comparison = Char
    else
    Comparison = ComboRow(ValidateIndex).Text & Char
    End If

    If rsMatchEntry.State = adStateOpen then
    rsMatchEntry.Close
    End If

    rsMatchEntry.Open strQuery, cn, adOpenKeyset

    If rsMatchEntry.BOF = false And rsMatchEntry.EOF = false then
    rsMatchEntry.MoveFirst
    Do Until rsMatchEntry.EOF
    Length = len(ComboRow(ValidateIndex).Text) + 1

    If rsMatchEntry.Fields(0).Value Like Comparison & "*" then
    ComboRow(ValidateIndex).Text = _
    Comparison & Right(rsMatchEntry.Fields(0).Value, len(rsMatchEntry.Fields(0).Value) - Length)
    cKeyAscii = 0
    ComboRow(ValidateIndex).SelStart = Length
    ComboRow(ValidateIndex).SelLength = _
    len(ComboRow(ValidateIndex).Text) - Length

    rsMatchEntry.Close
    Exit Sub
    End If

    rsMatchEntry.MoveNext

    Loop

    End If

    rsMatchEntry.Close

    End Sub





    The problem is this: the procedure only works with every other keystroke. The user hits "R" and the word "Radio" appears with the "adio" highlighted, like it is supposed to do. If the next keystroke is an "a" then rather than showing "Radio" with the "dio" highlighted, the combo box shows only "Ra." But if the third keystroke is a "d" then the box shows "Radio" again with the "io" highlighted.

    Anyone know what is causing this?


  2. #2
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: ComboBox text entry

    This code:

    SendKeys "{home}+{end}"



    will highlight all the text, so if you will plug it after the keystroke then whole text should become highlighted!


  3. #3
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: ComboBox text entry

    Hi,

    Put a Debug.Print statement in your ValidateEntry fn, after the lines:

    If len(ComboRow(ValidateIndex).Text) = 0 then
    Comparison = Char
    else
    Comparison = ComboRow(ValidateIndex).Text & Char
    End If
    Debug.print Comparison




    It turns out that, Comboxx.Text , on the Key Press event will be having a value that is the Text that was showing before the key press. I tested an example, and it shows
    that every other key-press only it takes the
    correct comparisoin string. ( and then it will match properly, and hilite too)

    So, what you need to is remember the previous search string or take only the string to the left of position at which key was pressed, or something like that. May be more logic is required.

    ANother thing, it may be easier to search in the Combo Box's List items itself. ( you need to populate it before ) instead of record search every time. Or have a local array of strings !

    RK

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

Featured