C# ——键盘指定输入
2021-05-16 08:29
标签:col trim 技术 null catch event har class 直线 防止输入有误 C# ——键盘指定输入 标签:col trim 技术 null catch event har class 直线 原文地址:https://www.cnblogs.com/eve612/p/14530365.html private void txtC_KeyPress(object sender, KeyPressEventArgs e)
{
txtKeyPress(sender, e);
}
private void txtKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
public bool IsValid()
{
if (string.IsNullOrEmpty(this.txtA.Text.Trim()) || string.IsNullOrEmpty(this.txtB.Text.Trim()) || string.IsNullOrEmpty(this.txtC.Text.Trim()))
{
MessageBox.Show("直线方程参数不能为空");
return false;
}
return true;