【发布时间】:2014-04-03 19:11:06
【问题描述】:
我正在使用 C# 以 win 形式编写桌面应用程序。我使用以下代码将我的文本框转换为数字文本框:
private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
{
//if (!char.IsControl(e.KeyChar)
// && !char.IsDigit(e.KeyChar)
// && e.KeyChar != '.')
// {
// e.Handled = true;
// }
if(!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
上面的代码只写一次,而不是每次都为表单中的每个文本框写一次,是不是蚂蚁设计模式或技术?
【问题讨论】:
-
如果您在单个表单上,只需将多个 TextBox 控件上的 KeyPress 事件设置为 txtPrice_KeyPress。
标签: c# .net events design-patterns