Answered by:
WPF textbox to accept decimal numbers

Question
-
I want textbox to accept the decimal numbers like 123.45 or 0.45
I have added a code in PreviewtextInput event of textbox
foreach (char ch in e.Text)
if (!Char.IsDigit(ch))
e.Handled = true;
But it allows only to accept the numbers and not decimals.
How I can allow the decimals numbers as it is a textbox of currency(amount).
Thanks
Answers
-
Hi Jegan,
Agree with Smith, Regular Expressions can make it easier.
Code snippets:
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$"); e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text)); }
Hope this helps.
Sincerely,
Bob Bao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM
-
Hi,
This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.
following are valid digits: 1, 3.3, 23.124, 1., 23.0
Following are invalid digit: . -->(i meant dot), .12
so use the following code:
foreach (char ch in e.Text)
if (!(Char.IsDigit(ch) || ch.Equals('.')))
{
e.Handled =
true;
}
- Jegan- Proposed as answer by Jegan Ravi Monday, May 24, 2010 5:30 PM
- Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM
All replies
-
Why not do it in OnKeyDown?
And you can check KeyEventArgs.Key to see if you should handle the event.
protected override void OnKeyDown(System.Windows.Input.KeyEventArgs aKeyEventArgs) { if (this._AllowedKeys.Count > 0) { aKeyEventArgs.Handled = !this._AllowedKeys.Contains(aKeyEventArgs.Key); } base.OnKeyDown(aKeyEventArgs); }
For decimals..
this._AllowedKeys.Add(System.Windows.Input.Key.Decimal);
this._AllowedKeys.Add(System.Windows.Input.Key.OemPeriod);
-
Hi,
This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.
following are valid digits: 1, 3.3, 23.124, 1., 23.0
Following are invalid digit: . -->(i meant dot), .12
so use the following code:
foreach (char ch in e.Text)
if (!(Char.IsDigit(ch) || ch.Equals('.')))
{
e.Handled =
true;
}
- Jegan- Proposed as answer by Jegan Ravi Monday, May 24, 2010 5:30 PM
- Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM
-
Hi,
you could use Decimal.TryParse(e.text, testvar) for this kind of check...
http://wpfglue.wordpress.com -
-
You may want to look into using RegularExpressions to validate your value.
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28VS.80%29.aspx
-
Hi Jegan,
Agree with Smith, Regular Expressions can make it easier.
Code snippets:
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$"); e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text)); }
Hope this helps.
Sincerely,
Bob Bao
Please remember to mark the replies as answers if they help and unmark them if they provide no help.- Marked as answer by Bob_BaoModerator Tuesday, June 1, 2010 1:07 AM