1

i'm making a program about finances management in WPF and I need textboxes like the ones the title describes.

Before you call this duplicate, I have searched in multiple sites and all the solutions proposed work fine with only numbers, but they don't seem to recognize my dot key or the comma key (I use Latin American qwerty keyboard), I dont know if the solutions I tried are region specific (because of the keyboards) or its something on my code.

So far I have tried:

This, with multiple Regex I have found around the internet that should have the same result

The same as before but manually comparing the e.KeyChar to Convert.ToChar(".")

Using the KeyDown event and multiple if (e.Key == Key.DX) (this worked for the dot, but not for the numbers and I tried the D0-D9 keys and the OEM ones)

None of these seem to work for me, and because I need to do math with the numbers and because of their purpose I need them to have the decimal dot (or comma)

Any help or ideas is appreciated.

1
  • What do you mean "Latin American" ? How is the name of the keyboard layout? Some layouts change the meaning of the numeric keyboard's . key to enter the culture's decimal separator. Are you using a layout that doesn't actually match your keyboard perhaps? For example, the British layout switches @ and ", so using keyboards purchased outside the UK will cause problems. That's not a software issue though Commented Jan 9, 2017 at 17:45

2 Answers 2

2

You could just use a standard textbox that allows any character, and then use an event handler on KeyDown or TextChanged that checks the text for illegal characters (anything other than a number, a comma, or a period). It would look something like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string result = "";
    char[] validChars = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '.' }; // these are ok
    foreach (char c in textBox1.Text) // check each character in the user's input
    {
        if (Array.IndexOf(validChars, c) != -1)
            result += c; // if this is ok, then add it to the result
    }

    textBox1.Text = result; // change the text to the "clean" version where illegal chars have been removed.
}
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, I think this approach was even easier than the ones I saw online! Thanks! This worked perfectly!
Using Regex would also work for the same thing, but since the number of choices is so limited this is fine.
@ElementalPete, what about invalid number formats? like 127.0.0.1? all chars are valid
@ASh I used this with a condition where it checks if a dot or comma already exists in the textbox and depending on that the code uses a different set of valid characters which doesnt include the dot or comma
@ASh - To your point, further validation would be needed either when the textbox loses focus or before calculations are performed or before the data is submitted. On the flipside, though, what about 1000.00? That number is perfectly valid and you couldn't type it if you enforced any advanced logic on TextChanged (because first you have to hit the "1", which doesn't end with a dot and two numbers, etc...) My approach should give the user quick feedback for good usability (ie- dollar signs are not allowed), but to your point it is not a complete validation solution.
2

Depending on the link you posted, I modified it for you

    private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        var ue = e.Source as TextBox;
        Regex regex;
        if (ue.Text.Contains("."))
        {
            regex = new Regex("[^0-9]+");
        }
        else
        {
            regex = new Regex("[^0-9.]+");
        }

        e.Handled = regex.IsMatch(e.Text);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.