更新时间:2023-11-28 15:02:10
文本框上的按键事件
Keypress event on the textbox
private void txtSimnr_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
}
以下是一些替代方案
1.使用MaskedTextBox(这是最简单的方法),在此处说明
http://msdn.microsoft.com/en-us/library/system. windows.forms.maskedtextbox.aspx [ ^ ]
2.在KeyPress事件中,如果按下的键不是必需的字符,则设置e.Handled = true
3.在KeyDown事件中,如果按下的键不是必需的字符,则设置
e.SuppressKeyPress = true
The following are some of the alternatives
1. Use MaskedTextBox (this is easiest approach) explained here
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx[^]
2. In the KeyPress event if the Key pressed is not the required character then set e.Handled = true
3. In the KeyDown event if the key pressed is not the required character then set
e.SuppressKeyPress = true