S
Shan1986
Guest
Hallo,
I have found this code on another form given by Karen and i am trying to modify a bit with few extra properties but can not get it right.
I have three extra properties
1. Set number of digits allowed (with minus sign "-")
2. Specify if the textbox for integers only or decimals
3. number of decimals points
Imports System.Text.RegularExpressions
Imports System.Windows
Public Class numericTextbox
Inherits TextBox
Const WM_PASTE As Integer = &H302
Public Property Digits As Integer = 3 '//Set number of digits allowed
Public Property DecimalValue As Boolean '//Set Integers or Decimal
Public Property DecimalPoints As Integer = 2 '//Set Number of Decimal points
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim deci = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
Dim pattern As String = "^(([0-9]{1," & Digits & "})|([0-9]{1," & Digits & "}(\.){1,1}([0-9]){0,1}))$"
Dim Value As String = Me.Text
Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
Value = Value.Insert(Me.SelectionStart, e.KeyChar)
e.Handled = CBool(Value.LastIndexOf("-") > 0) Or Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse (e.KeyChar = "."c And Not Me.Text.Contains(".") Or e.KeyChar = "."c And Me.SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And Me.SelectionStart = 0))
'MyBase.OnKeyPress(e)
While Not Regex.IsMatch(Value, pattern) AndAlso Not Value = ""
Value = Value.Substring(0, Value.Length - 1)
End While
Me.Text = Value
''** Set the caret to the end of the string in the textbox
Me.Select(Me.Text.Length, 0)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PASTE Then
Dim Value As String = Me.Text
Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
Value = Value.Insert(Me.SelectionStart, Clipboard.GetText)
Dim result As Decimal = 0
If Not Decimal.TryParse(Value, result) Then
Return
End If
End If
MyBase.WndProc(m)
End Sub
End Class
With the code above when i type every keypress comes twice. any help? thanks.
Continue reading...
I have found this code on another form given by Karen and i am trying to modify a bit with few extra properties but can not get it right.
I have three extra properties
1. Set number of digits allowed (with minus sign "-")
2. Specify if the textbox for integers only or decimals
3. number of decimals points
Imports System.Text.RegularExpressions
Imports System.Windows
Public Class numericTextbox
Inherits TextBox
Const WM_PASTE As Integer = &H302
Public Property Digits As Integer = 3 '//Set number of digits allowed
Public Property DecimalValue As Boolean '//Set Integers or Decimal
Public Property DecimalPoints As Integer = 2 '//Set Number of Decimal points
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim deci = Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
Dim pattern As String = "^(([0-9]{1," & Digits & "})|([0-9]{1," & Digits & "}(\.){1,1}([0-9]){0,1}))$"
Dim Value As String = Me.Text
Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
Value = Value.Insert(Me.SelectionStart, e.KeyChar)
e.Handled = CBool(Value.LastIndexOf("-") > 0) Or Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse (e.KeyChar = "."c And Not Me.Text.Contains(".") Or e.KeyChar = "."c And Me.SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And Me.SelectionStart = 0))
'MyBase.OnKeyPress(e)
While Not Regex.IsMatch(Value, pattern) AndAlso Not Value = ""
Value = Value.Substring(0, Value.Length - 1)
End While
Me.Text = Value
''** Set the caret to the end of the string in the textbox
Me.Select(Me.Text.Length, 0)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PASTE Then
Dim Value As String = Me.Text
Value = Value.Remove(Me.SelectionStart, Me.SelectionLength)
Value = Value.Insert(Me.SelectionStart, Clipboard.GetText)
Dim result As Decimal = 0
If Not Decimal.TryParse(Value, result) Then
Return
End If
End If
MyBase.WndProc(m)
End Sub
End Class
With the code above when i type every keypress comes twice. any help? thanks.
Continue reading...