Numeric TextBox in C# - WPF [duplicate]

*爱你&永不变心* 提交于 2019-11-29 14:40:54

问题


This question already has an answer here:

  • How do I get a TextBox to only accept numeric input in WPF? 30 answers

i want to create a textbox in WPF that only accept numbers... i've reaserched and people say to use keypress event or masked textbox, but they are in windows forms...


回答1:


For WPF:

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

For Windows Forms:

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


来源:https://stackoverflow.com/questions/17505008/numeric-textbox-in-c-sharp-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!