www.pudn.com > NumberBox.rar > NumberBox.ctl
VERSION 5.00
Begin VB.UserControl NbrTextBox
ClientHeight = 405
ClientLeft = 0
ClientTop = 0
ClientWidth = 1740
ScaleHeight = 405
ScaleWidth = 1740
ToolboxBitmap = "NumberBox.ctx":0000
Begin VB.TextBox txtNumberBox
Height = 405
Left = 0
TabIndex = 0
Top = 0
Width = 1755
End
End
Attribute VB_Name = "NbrTextBox"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Public Event NumberBoxKeyPress()
Private mbCanHaveDecimals As Boolean
Private mbCanBeNegative As Boolean
Private mintMaxDecimals As Integer
Public Property Get CanHaveDecimals() As Boolean
Attribute CanHaveDecimals.VB_Description = "Returns/sets a value which determines whether decimals are allowed in the number."
CanHaveDecimals = mbCanHaveDecimals
End Property
Public Property Let CanHaveDecimals(ByVal bTF As Boolean)
mbCanHaveDecimals = bTF
PropertyChanged "CanHaveDecimals"
End Property
Public Property Get CanBeNegative() As Boolean
Attribute CanBeNegative.VB_Description = "Returns/sets a value which determines whether the number can be negative."
CanBeNegative = mbCanBeNegative
End Property
Public Property Let CanBeNegative(ByVal bTF As Boolean)
mbCanBeNegative = bTF
PropertyChanged "CanBeNegative"
End Property
Public Property Get MaxDecimals() As Integer
Attribute MaxDecimals.VB_Description = "Returns/sets the maximum allowable number of decimal places. Ignored if DecimalsOK is False."
MaxDecimals = mintMaxDecimals
End Property
Public Property Let MaxDecimals(ByVal intDecimals As Integer)
If intDecimals < 1 Then
intDecimals = 1
End If
mintMaxDecimals = intDecimals
PropertyChanged "MaxDecimals"
End Property
Private Sub txtNumberBox_KeyPress(KeyAscii As Integer)
Dim intPos As Integer
' Raise a KeyPress event whenever the user presses
' a key
RaiseEvent NumberBoxKeyPress
Const MINUS_SIGN = "-"
Const DECIMAL_POINT = "."
Select Case KeyAscii
Case 8 ' Backspace
' Backspace is always OK
Case 48 To 57 ' Backspace, and 0 to 9
' These are always OK unless the number of decimals exceeds
' the allowed amount
intPos = InStr(1, txtNumberBox, DECIMAL_POINT)
If intPos > 0 Then
If Len(txtNumberBox.Text) - intPos = MaxDecimals Then
' We already are at the maximum number of decimals
KeyAscii = 0
Beep
End If
End If
Case 46 ' Decimal-point
If CanHaveDecimals Then
If InStr(1, txtNumberBox, DECIMAL_POINT) Then
' There is already a DECIMAL_POINT
KeyAscii = 0
Beep
Else
KeyAscii = KeyAscii
End If
Else
KeyAscii = 0
Beep
End If
Case 45 ' MINUS_SIGN-sign
If CanBeNegative Then
If InStr(1, txtNumberBox, MINUS_SIGN) Or txtNumberBox.SelStart <> 0 Then
' There is already a minus-sign or the minus-sign is not in pos 1
KeyAscii = 0
Beep
Else
KeyAscii = KeyAscii
End If
Else
KeyAscii = 0
Beep
End If
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Private Sub UserControl_Initialize()
CanHaveDecimals = True
CanBeNegative = True
MaxDecimals = 2
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
CanHaveDecimals = PropBag.ReadProperty("CanHaveDecimals", True)
CanBeNegative = PropBag.ReadProperty("CanBeNegative", True)
MaxDecimals = PropBag.ReadProperty("MaxDecimals", 2)
End Sub
Private Sub UserControl_Resize()
txtNumberBox.Width = UserControl.Width
txtNumberBox.Height = UserControl.Height
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "CanHaveDecimals", CanHaveDecimals, True
PropBag.WriteProperty "CanBeNegative", CanBeNegative, True
PropBag.WriteProperty "MaxDecimals", MaxDecimals, 2
End Sub