在C# - WPF中的数字文本框

41 浏览
0 Comments

在C# - WPF中的数字文本框

这个问题已经在这里有了答案:

在WPF中,如何让TextBox只接受数字输入?

我想在WPF中创建一个只接受数字的文本框...

我已经调研过了,人们说要使用keypress事件或掩码文本框,但它们都是在Windows窗体中...

admin 更改状态以发布 2023年5月22日
0
0 Comments

对于WPF:

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!char.IsDigit(e.Text, e.Text.Length - 1))
        e.Handled = true;
}

对于Windows表单:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) )
        e.Handled = true;
}

0