Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric TextBox in C# - WPF [duplicate]

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...

like image 996
Kourosh Avatar asked Jul 06 '13 16:07

Kourosh


1 Answers

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;
}
like image 135
Maciek Avatar answered Oct 24 '22 17:10

Maciek