我有一个文本框,用户输入一个数字,但我怎么能这样做,如果他们输入'.' 之后它只允许2位小数?
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
aqu*_*nas 10
只需添加:
if (Regex.IsMatch(textBox1.Text, @"\.\d\d")) {
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
到你的功能结束
| 归档时间: |
|
| 查看次数: |
33760 次 |
| 最近记录: |