Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a text control that let the program accept or reject changes
KeywordsTextBox, accept, reject, cancel, preview, PreviewText
CategoriesControls, ActiveX Controls
 
The key to this control is the NewValue function. It determines what the control's new value will be if the program allows the change. This function's parameters give the numeric code of the key pressed and a Boolean indicating whether the key is the Delete key.
 
' See what the new value would be
' if this change is allowed.
Private Function NewValue(ByVal KeyValue As Integer, ByVal _
    is_delete As Boolean) As String
' Useful ASCII codes.
Const ASC_CTRL_V = 22
Const ASC_CTRL_X = 24
Const FIRST_ASC = 32    ' 1st visible char.
Const LAST_ASC = 126    ' Last visible char.

Dim old_txt As String
Dim part1 As String
Dim part2 As String
Dim part3 As String

    old_txt = txtInput.Text

    ' Find the pieces before and after the selection.
    part1 = Left$(old_txt, txtInput.SelStart)
    part3 = Right$(old_txt, Len(old_txt) - _
        txtInput.SelStart - txtInput.SelLength)

    ' Calculate the new text.
    part2 = ""
    If is_delete Then
        ' This is a delete character.
        ' If no text is selected, delete the
        ' following character (if there is one).
        If (txtInput.SelLength <= 0) And _
           (Len(part3) > 0) _
        Then
            part3 = Mid$(part3, 2)
        End If

        ' If text is selected or part3 is blank,
        ' just leave part2 = "".
    Else
        ' This is not a delete character.
        ' See what it is.
        Select Case KeyValue
            Case vbKeyBack      ' Backspace.
                ' If no text is selected, delete the
                ' previous character (if there is one).
                If (txtInput.SelLength <= 0) And _
                   (Len(part1) > 0) _
                Then
                    part1 = Left$(part1, Len(part1) - 1)
                End If
                ' If text is selected or part1 is
                ' blank, leave part2 = "".

            Case ASC_CTRL_V
                ' Paste the clipboard's text over
                ' the selected text.
                part2 = Clipboard.GetText(vbCFText)

            Case ASC_CTRL_X
                ' Leave part2 = "".

            Case FIRST_ASC To LAST_ASC
                ' Use the visible character typed
                ' for the middle string.
                part2 = Chr$(KeyValue)

            Case Else
                ' Assume other non-visible keys
                ' like ^C will not change the text.
                NewValue = old_txt
                Exit Function
        End Select
    End If

    ' Build the result.
    NewValue = part1 & part2 & part3
End Function
 
The control's TextBox calls this function in its KeyDown and KeyPress event handlers. It then raises the BeforeChange event to let the program see the new value. If the program changes the event's Cancel parameter to True, the program disallows the change.
 
' Watch for Delete keys.
Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As _
    Integer)
Dim new_value As String
Dim Cancel As Boolean

    If KeyCode = vbKeyDelete Then
        ' See what the new value would be.
        new_value = NewValue(KeyCode, True)

        ' Tell the program.
        RaiseEvent BeforeChange(new_value, Cancel)
        If Cancel Then KeyCode = 0
    End If
End Sub

' Validate key presses.
Private Sub txtInput_KeyPress(KeyAscii As Integer)
Dim new_value As String
Dim Cancel As Boolean

    ' See what the new value would be.
    new_value = NewValue(KeyAscii, False)

    ' Tell the program.
    RaiseEvent BeforeChange(new_value, Cancel)
    If Cancel Then KeyAscii = 0
End Sub
 
When the test program receives the BeforeChange event, it prompts the user to see if the change should be allowed.
 
Private Sub PreviewText1_BeforeChange(ByVal new_value As _
    String, Cancel As Boolean)
    Cancel = _
        (MsgBox("Accept new value """ & new_value & """?", _
            vbQuestion Or vbYesNo, "Accept Change?") _
        = vbNo)
End Sub
 
For the code to 101 ActiveX controls, see my book Custom Controls Library.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated