WPF PreviewKeyDown错误

时间:2012-06-08 06:35:02

标签: wpf textbox

我有以下代码。 当我在“。”之后键入2个以上的数字时会出现一个小问题,它会继续提示我(“不超过两位小数。”)...

当我有.XX并点击退格键时,它也会提示我。

如何在“。”之后自动删除第3个数字?

private void textbox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        MessageBox.Show("No space allowed.");
        e.Handled = true;
    }
    string[] array = textbox1.Text.Split(new char[] { '.' });
    if (array.Length == 2)
    {
        if (array[1].Length == 2)
        {
            MessageBox.Show("No more than two decimal places.");
            e.Handled = true;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我用过

if (e.Key != Key.Back)

停止退格问题。

现在没关系。