限制文本框仅允许浮点型数据
private void txt_Double_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == '\b') return;
if (e.KeyChar == '.')
{
//当前输入字符为小数点时,检验文本框未选中的文本中包含小数点时,不输入小数点;
var txt = ((TextBox)sender);
var text = txt.Text;
if (!string.IsNullOrWhiteSpace(txt.SelectedText))
text = text.Replace(txt.SelectedText, string.Empty);
if (!text.Contains(".") && !string.IsNullOrWhiteSpace(text))
return;
}
if (char.IsDigit(e.KeyChar)) return;
e.Handled = true;
}