0

I am working on a decimal number (2 place) only textbox. e.g. 123.11 and -945.56.

I have searched some post and found some solution like
How do I get a TextBox to only accept numeric input in WPF?
C# how to restrict textbox decimal places to 2?

Then I make a class contain the two function with some adjustment. I can restrict the user input only 2 decimal places number. However, I can only restrict the user place decimal number with any decimal places like 123.12345678 and I cannot stop them to paste again, the result can be 123.11123.11 or -945.56-945.56 which are not decimal number.

I try to use MessageBox.Show(text) to find the actual input but I get System.InvalidOperationException.

I have no idea to deal with the TextBox Paste Event. For the worst case, I might have to disable the Paste command...

My code are below, as my experience in programming is few, there might be some bug.

<TextBox    x:Name="textBox1"
            PreviewTextInput="textBox1_PreviewTextInput"
            />
DataObject.AddPastingHandler(textBox1, Class_TextBox_Allow.TextBoxPasting_decimal_p2_v2);

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    Class_TextBox_Allow.TextInput_decimal_p2_v2(sender, e);
}
private static readonly Regex _regex_decimal = new Regex("[^0-9.-]+"); //regex that matches disallowed text

private static bool IsTextAllowed_decimal(string text)
{
    return !_regex_decimal.IsMatch(text);
}

private static bool IsText_decimal(string text)
{
    //MessageBox.Show(text);
    decimal text_decimal = 0;
    //return decimal.TryParse(text, out decimal text_decimal);
    if (decimal.TryParse(text, out text_decimal))
    {
        return true;
    }
    else if (decimal.TryParse(text + "0", out text_decimal))
    {
        return true;
    }
    else
    {
        return false;
    }
}

public static void TextBoxPasting_decimal_p2_v2(object sender, DataObjectPastingEventArgs e)
{
    if (e.DataObject.GetDataPresent(typeof(String)))
    {
        String text = (String)e.DataObject.GetData(typeof(String));
        //MessageBox.Show(text)
        if (
            !IsTextAllowed_decimal(text) |
            !IsText_decimal(text) |
            Regex.IsMatch(text, "^\\d*\\.\\d{2}$") |
            Regex.IsMatch(text, "^-\\d*\\.\\d{2}$")
            )
        {
            e.CancelCommand();
        }
    }
    else
    {
        e.CancelCommand();
    }
}

public static void TextInput_decimal_p2_v2(object sender, TextCompositionEventArgs e)
{
    TextBox textBox = sender as TextBox;
    int textBox_Index = textBox.SelectionStart;
    string text = textBox.Text.Insert(textBox_Index, e.Text);
    if (!string.IsNullOrWhiteSpace(text))
    {
        if (
            !IsTextAllowed_decimal(e.Text) |
            !IsText_decimal(text) |
            Regex.IsMatch(text, "^\\d*\\.\\d{3}$") |
            Regex.IsMatch(text, "^-\\d*\\.\\d{3}$")
            )
        {
            e.Handled = true;
        }
    }
    else
    {
        e.Handled = true;
    }
}
testcb00
  • 43
  • 1
  • 6

0 Answers0