我正在寫使用C#。我使用下面的代碼到我的文本轉換爲數字文本框在Win形式的桌面應用程序:避免重複的代碼在C#
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。 – FodderZone