Groups | Blog | Home
all groups > vb.net controls > april 2005 >

vb.net controls : Creating a NumericTextbox


Fia
4/22/2005 12:00:00 AM
Down below is code for creating a control, NumericTextbox
Public Class NumericTextbox
Inherits System.Windows.Forms.TextBox
Private Sub NumericTextbox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45 'minus tecken
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
Case 46 'punkt
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
End Class

which only allow users to enter digits, not characters.

But if I use this control on a form and uses the control NumericTextbox:s
event KeyPress like this

Private Sub NumericTextbox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles NumericTextbox1.KeyPress
Me.NumericTextbox1.Text = e.KeyChar
End Sub

you can put characters in the textbox. The KeyPress event shouldn't be
available in the left-hand drop-down box for the NumericTextbox1, since we
have used the KeyPress event for our control to make it just allow digits,
not characters. Is there a way to remove the KeyPress event from the
left-hand drop-down box for the NumericTextbox, so the users of the control
NumericTextbox can't use the KeyPress event or is there another way to make
the KeyPress event unavailable for users of the NumricTextbox???

Please Help

Fia

AddThis Social Bookmark Button