|
|
Title | Make an ActiveX control that allows the user to enter only valid integer within a certain range |
Description | This example shows how to make an ActiveX control that allows the user to enter only valid integer within a certain range in Visual Basic 6. It catches the KeyPress and KeyDown events and determines whether the result would be valid if it allows the key. |
Keywords | field, integer, data validation, validation |
Categories | Controls, ActiveX, ActiveX Controls, Software Engineering |
|
|
The program uses a TextBox. The KeyPress and KeyDown event handlers call subroutine CheckText. CheckText checks whether the change is allowed by calling function ValidChange. ValidChange uses the NewValue function to see what the control's value will be if the change is allowed.
|
|
' Watch for Delete keys.
Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As _
Integer)
If KeyCode = vbKeyDelete Then CheckText KeyCode, True
End Sub
' Validate key presses.
Private Sub txtInput_KeyPress(KeyAscii As Integer)
CheckText KeyAscii, False
End Sub
' Set KeyValue = 0 if the result of this key press
' would be invalid.
Private Sub CheckText(ByRef KeyValue As Integer, ByVal _
is_delete As Boolean)
If Not ValidChange(KeyValue, is_delete) _
Then KeyValue = 0
End Sub
' See if this change is valid.
Private Function ValidChange(ByVal KeyValue As Integer, _
ByVal is_delete As Boolean) As Boolean
Dim str_value As String
Dim is_valid As Boolean
Dim int_value As Integer
' See what the new value will be.
str_value = NewValue(KeyValue, is_delete)
' See if the new value is valid.
If Len(str_value) = 0 Then
' The text is blank. Allow it.
is_valid = True
ElseIf str_value = "-" Then
' The text is "-". It might be part of
' a negative integer. Allow it if
' m_MinValue < 0.
is_valid = (m_MinValue < 0)
Else
' See if the text is an integer.
On Error Resume Next
int_value = CInt(str_value)
If Err.Number <> 0 Then
' The text is not an integer.
is_valid = False
Else
' See if the value is within bounds.
is_valid = _
(int_value >= m_MinValue) And _
(int_value <= m_MaxValue)
End If
On Error GoTo 0
End If
' Return the result.
ValidChange = is_valid
End Function
|
|
Function NewValue figures out what the field's new value will be.
|
|
' 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 section.
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
|
|
This control also replaces the standard TextBox popup menu with one of its own so the user cannot right click and paste text without validation.
You can use similar techniques to perform other "after the fact" validations on fields.
|
|
|
|
|
|