C#winform控制键盘只输入小数的简洁写法?

hsie168518 hsie168518 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
比如一个窗体中有(textbox1,textbox2,textbox3),我要给控制他们只能输入数字,可以
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Back))
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Back))
{
e.Handled = true;
}
}

private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{

if ((e.KeyChar >= '0' && e.KeyChar <= '9') || ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Back))
{
e.Handled = true;
}
}

但是这样写不专业,我只想写一个方法,

然后 private void InitializeComponent(){}里面使用代理,完成相同功能,怎么写?
...全文
评论 17
收藏 已收藏
分享
返回顶部
hsie168518
C# 111,129 成员 642,539 内容