Search code examples
textboxvb6keypressundo

How do I find what a TextBox's contents will be after pressing Ctrl + Z, before it occurs?


I am writing a small function that will calculate the contents of a TextBox after the KeyPress event, but during the KeyPress event. I know, it sounds strange. :)

The reason for this is because I have been given a project at work, where I am to fix any errors in a current program.

There is currently a TextBox, which triggers a search on a SQL Server, in the KeyPress event. This searches for the current contents of the TextBox concatenated with Chr(KeyAscii)

This means that if your TextBox contains Hello and your cursor is at the end of the word it will work correctly, but if you have the o selected and press a it will search for Helloa instead of Hella.

So to correct, this I have come up with the following function

Private Function TextAfterKeyPress(Ctrl As Control, KeyAscii As Integer) As String
    Dim strUnSelLeft As String
    Dim strUnSelRight As String
    Dim strSel As String
    Dim strMid As String

    With Ctrl
        strUnSelLeft = ""
        strUnSelRight = ""
        strMid = .Text

        Select Case KeyAscii
            Case 1                                           ' Ctrl + A
                ' No change to text
            Case 3                                           ' Ctrl + C
                ' No change to text
            Case 8                                           ' BackSpace
                If .SelStart = 0 Then
                    ' No change to text
                Else
                    If .SelLength = 0 Then
                        strUnSelLeft = Left(.Text, .SelStart - 1)
                        strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                        strMid = ""
                    Else
                        strUnSelLeft = Left(.Text, .SelStart)
                        strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                        strMid = ""
                    End If
                End If
            Case 9                                           ' Tab
                ' No change to text
            Case 13                                          ' Return
                ' No change to text
            Case 22                                          ' Ctrl + V
                strUnSelLeft = Left(.Text, .SelStart)
                strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                strMid = Clipboard.GetText
            Case 24                                          ' Ctrl + X
                If .SelLength = 0 Then
                    ' No change to text
                Else
                    strUnSelLeft = Left(.Text, .SelStart)
                    strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                    strMid = ""
                End If
            Case 26                                          ' Ctrl + Z





            Case 27                                          ' Esc
                ' No Change to text
            Case 137, 153, 160, 169, 188, 189, 190, 215, 247 ' Disallowed Chars
                ' No Change to text
            Case 128 To 255                                  ' Allowed non standard Chars
                strUnSelLeft = Left(.Text, .SelStart)
                strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                strMid = Chr(KeyAscii)
            Case 32 To 127                                   ' Standard Printable Chars
                strUnSelLeft = Left(.Text, .SelStart)
                strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
                strMid = Chr(KeyAscii)
            Case Else
        End Select

        TextAfterKeyPress = strUnSelLeft & strMid & strUnSelRight
    End With
End Function

Now for the section where it says "Case 26", it will perform an undo action, and then the search won't work correctly again.

Is there any way to find out what the contents of the TextBox will be when you press Ctrl + Z, but during the KeyPress in which it happens? The KeyPress event fires before the content of the TextBox changes so I would need to find out what is in the Undo buffer so that I can run a correct search.

Can anyone point me in the right direction?


Solution

  • I have worked with textbox APIs a bit ..And i don't think you can deal with its UNDO buffer due to its limitation.. but something you can do is to manually send an UNDO then read the textbox content.

    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 EM_UNDO = &HC7  
    

    ,

    Case 26  
        KeyAscii = 0  
        SendMessage .hwnd, EM_UNDO, ByVal 0, ByVal 0  
        strMid = .Text  
    

    If you only need to read the text and not to change it you can send an another UNDO msg to get the text to its previous content.