C# 禁止非数字输入TextBoox
对TextBox设置输入事件:
private void Num_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
TextBox tb = (TextBox)sender;
int count = tb.Text.Length;
bool yes;
Regex re = new Regex("[^0-9]");
yes = re.IsMatch(e.Text);
if (!yes)
{
if (count == 1 && tb.Text == "0")
yes = true;
}
e.Handled = yes;
}