我已经为我的所有
分配了以下方法 private void textBox18_KeyPress_1(object sender, KeyPressEventArgs e)
{
char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != a))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == a) && ((sender as TextBox).Text.IndexOf(a) > -1))
{
e.Handled = true;
}
}
它基本上允许一个小数分隔符(任何类型),并且只允许数字。 我宁愿禁用"粘贴"以及这种方法。这可能吗?
我知道有些用户可能会在此重定向我
how to disable copy, Paste and delete features on a textbox using C#
但我的代码无法识别e.Control
和e.KeyCode
。即使我在表单开头添加using Windows.Forms
。即便如此,我也不知道这些解决方案是否有效。
答案 0 :(得分:4)
KeyPress
event中没有这些属性:
KeyPress事件不是由空格和退格之外的非字符键引发的;但是,非字符键会引发KeyDown和KeyUp事件。
订阅KeyDown
事件,您可以访问用户碰巧按下的任何修改键(control,alt,shift)。
private void textBox18_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
{
// cancel the "paste" function
e.SuppressKeyPress = true;
}
}