WPF Numeric Textbox |
Introduction
This blog WPF Textbox Numeric Only provides some code snippets to create a WPF numeric textbox that will accept numeric values only. This WPF textbox will avoid the to enter strings and special characters and also will restrict to copy past non-numeric values.
Getting Started
WPF provides a special privilege to restrict any key value to enter in the Textbox, we will take benefit and will restrict in C# WPF textbox to enter string values or special characters.
There are two ways to restrict non-numeric values to the textbox. One is using the PreiewTextInput event and the other is using the PreviewKeyDown event. The PreiewTextInput occurs when this element gets the text in a device-independent manner and PreviewKeyDown Occurs when a key is pressed while the focus is on this element.
WPF Numeric Textbox with PreiewTextInputThe following code snippets create a Numeric Textbox by using the PreiewTextInput and regular expression (C# Regex). This C# regex validates the input coming from the textbox. See the XAML and C# code to know more.
<TextBox Text="Numeric TextBox" PreviewTextInput="PreviewTextInput"/>
private void PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
Copy the above XAML code and paste inside the windows XAMPL code also the C# code into the Windows.xaml.cs file to see the result.
Restrict String Value while Copy PasteThe following code snippets don’t allow string values while copy paste in the textbox. It uses the WPF Textbox DataObject.Pasting Attached Event, this event is intended to provide an opportunity for applications to inspect the contents of the DataObject prior to the paste operation, change, remove or add data formats, or cancel the entire copy operation by calling CancelCommand. Copy the above XAML code and paste inside the windows XAMPL code also the C# code into the Windows.xaml.cs file to eximine the result.
<TextBox Text="Numeric TextBox" PreviewTextInput="PreviewTextInput" DataObject.Pasting="TextBox_Pasting"/>
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
string text = (string)e.DataObject.GetData(typeof(String));
Regex regex = new Regex("[^0-9]+");
if (regex.IsMatch(text) ==true)
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
WPF Numeric Textbox with PreviewKeyDown
The following code snippets use the PreviewKeyDown event to check the key entered into the textbox and restring the key based on the condition.
<TextBox BorderBrush="DarkCyan" BorderThickness="2" x:Name="textBox" FontSize="30" FontWeight="ExtraBold" Text="1234567890" PreviewKeyDown="textBox_PreviewKeyDown" />
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key>=Key.D0 && e.Key<=Key.D9)
||(e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
|| e.Key==Key.Back)
e.Handled = false;
else
e.Handled = true;
}
Summary
There may various way available to create a WPF numberic textbox, here we discussed two way to restrict string values. Hope that this blog is help full and understood the logic to accept numeric only in a WPF Textbox.
Related Articles
- Enable WPF Textbox Multiline Text
- Round Corner TextBox in WPF
- Round Corner TextBox With Border Effect
- Data Validation in WPF
Thanks
good one, such a helpful content
ReplyDelete