This example shows the way to stop a user from providing alphabetical or symbolic inputs during a WPF input field. this instance is sort of easy however may be terribly helpful to stop any run time error. Suppose, you’ve got developed associate degree application that keeps track of the daily financial gain and expense of a store. To do that, you’ve got to feature and figure values in numerous places. however a user may give associate degree alphabetical input within the textbox and eventually the appliance would crush. to stop that, you’ll merely write a code to prevent users from providing anything instead of a numeric input.
First, Write a new function that takes one string input and two lines of code inside it.
public static bool onlyNumeric(string text)
Regex regex = new Regex(“[^0-9.-]+”); //regex that allows numeric input only
return !regex.IsMatch(text); //
Then add a textbox in a wpf application and Create a new event:
A new function for that event would be created in the cs page automatically. Now write the following line inside the newly created function:
e.Handled = !onlyNumeric(e.Text);
Also add the below line at the top of the cs page:
All done. Now run the application and you will see that the WPF texbox would take numeric input only.