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;
}

posted @ 2020-06-15 13:44  幸运(● ̄(エ) ̄●)  阅读(309)  评论(0编辑  收藏  举报
Document