hi,
i think the follwing code will help
Ashu
'******************************************
Public Class NumericTextBox
Inherits System.Windows.Forms.TextBox
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl1 overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'NumericTextBox
'
Me.AccessibleRole = System.Windows.Forms.AccessibleRole.Application
Me.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
End Sub
#End Region
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 '0-9, Backspace, Enter KEY
'Do nothing
Case 45 'Minus sign (-)
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46 'Dot (.)
If InStr(Me.Text, ".") 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
Private Sub NumericTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
End Sub
End Class
|