|
|
Title | Make a numeric field using locked TextBoxes |
Keywords | numeric field, KeyPress, locked TextBox |
Categories | Controls |
|
|
By Martyn Sutcliffe.
Simply set the textbox "Locked" property to True. The text box still receives
the KeyPress event (giving the char code), but dosn't do anything with it.
The user can copy out of the Textbox, but not paste into it.
The developer then has to update the text property how they feel fit.
|
|
Private Sub Text1_KeyPress(KeyAscii As Integer)
'------ Example of restricting integers in text boxes
'------ Decs
Dim sTempText As String
'------ Get initial settings
sTempText = Text1.Text
'------ Show the ascii key pressed
Form1.Caption = "Test Integer Text Box: (Key:" & _
CStr(KeyAscii) & ")"
'------ Test the Key Pressed
If ((KeyAscii > 47) And (KeyAscii < 58)) Then
'------ Add the num to the end, limit to 9 chars
sTempText = Left(sTempText & Chr$(KeyAscii), 9)
'------ Enforce no leading zeros e.g 002
' And set text
Text1.Text = CStr(CLng(sTempText))
ElseIf ((KeyAscii = 8) And (Len(sTempText) > 0)) Then
'------ Remove the last num
sTempText = Left$(sTempText, Len(sTempText) - 1)
'------ If Zero length make zero
If (Len(sTempText) = 0) Then
sTempText = "0"
End If
'------ Set text
Text1.Text = sTempText
End If
'------ set the cursor to the last pos
Text1.SelLength = 0
Text1.SelStart = Len(Text1.Text)
End Sub
|
|
[Note that my book Custom Controls Library shows how to make field controls that do this sort of thing, though not this way.]
|
|
|
|
|
|