I need to be able to lock down the valid characters in a textbox, I presently have a regex which I can check each character against such as
我需要能够锁定文本框中的有效字符,我现在有一个正则表达式,我可以检查每个字符,如
[A-Za-z]
would lock down to just Alpha characters.
将锁定为只有Alpha字符。
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Back)
{
base.OnKeyPress(e);
return;
}
if (String.IsNullOrEmpty(this._ValidCharExpression))
{
base.OnKeyPress(e);
}
else
{
bool isValidChar = Regex.Match(e.KeyChar.ToString(),this._ValidCharExpression).Success;
if (isValidChar)
{
base.OnKeyPress(e);
}
else
{
e.Handled = true;
}
}
}
I had placed the regex code in the OnKeyPress code, but I wat to allow all special keys, such as Ctrl-V, Ctrl-C and Backspace to be allowed.
我已经将正则表达式代码放在OnKeyPress代码中,但是我允许允许所有特殊键,例如Ctrl-V,Ctrl-C和Backspace。
As you can see I have the backspace key being handled. However, Ctrl-V, for example cannot see the V key because it runs once for the ctrl key but does not see any modifiers keys.
如您所见,我正在处理退格键。但是,Ctrl-V例如无法看到V键,因为它对ctrl键运行一次但没有看到任何修饰键。
What is the best way to handle this situation?
处理这种情况的最佳方法是什么?
5 个解决方案
#1
0
You can use one of the OnKeyPress / OnKeyUp / OkKeyDown events and then use the Char.IsLetter method to check that the entered key is a letter.
您可以使用OnKeyPress / OnKeyUp / OkKeyDown事件之一,然后使用Char.IsLetter方法检查输入的键是否为字母。
#2
4
MaskedTextBox may be right for you.
MaskedTextBox可能适合您。
You can also look at the FilterTextBox over at CodeProjct. You can use it (or the approach described) to do what you intend. The basic idea is to cancel the change before it is becoming visible (via an OnTextChanging event).
您还可以在CodeProjct上查看FilterTextBox。你可以使用它(或描述的方法)来做你想要的。基本思想是在变化变为可见之前取消变更(通过OnTextChanging事件)。
#3
2
What if you put the validation in OnTextChanged instead of OnKeyPress, but each time it passes validation you save the value to a variable? Then you can revert if the user pastes or types an incorrect string, as well as give some other UI hint that something was invalid (e.g. set a Label's text).
如果您将验证放在OnTextChanged而不是OnKeyPress,但每次通过验证时将值保存到变量,该怎么办?然后,如果用户粘贴或键入不正确的字符串,您可以还原,并提供一些其他UI提示,表明某些内容无效(例如,设置Label的文本)。
#4
1
Why don't you put the check for valid characters in the OnTextChanged event
为什么不在OnTextChanged事件中检查有效字符
and then deal with the Ctrl+C, Ctrl+V in the on key down
然后处理按键上的Ctrl + C,Ctrl + V键
Also you can use the e.ModifierKeys == Keys.Control to test for control keys
您还可以使用e.ModifierKeys == Keys.Control来测试控制键
http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx
#5
0
The solution that I have come up with is to check the keys in the OnKeyDown event and then setting a flag if the keypress should be handled, which is then check in the OnKeyPress event.
我想出的解决方案是检查OnKeyDown事件中的键,然后设置一个标志,如果应该处理keypress,然后检查OnKeyPress事件。
protected override void OnKeyDown(KeyEventArgs e)
{
Keys keyCode = (Keys)e.KeyValue;
base.OnKeyDown(e);
if ((e.Modifiers == Keys.Control) ||
(e.Modifiers == Keys.Control) ||
(keyCode == Keys.Back) ||
(keyCode == Keys.Delete))
{
this._handleKey = true;
}
else
{
// check if the key is valid and set the flag
this._handleKey = Regex.Match(key.ToString(), this._ValidCharExpression).Success;
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (this._handleKey)
{
base.OnKeyPress(e);
this._handleKey = false;
}
else
{
e.Handled = true;
}
}
#1
0
You can use one of the OnKeyPress / OnKeyUp / OkKeyDown events and then use the Char.IsLetter method to check that the entered key is a letter.
您可以使用OnKeyPress / OnKeyUp / OkKeyDown事件之一,然后使用Char.IsLetter方法检查输入的键是否为字母。
#2
4
MaskedTextBox may be right for you.
MaskedTextBox可能适合您。
You can also look at the FilterTextBox over at CodeProjct. You can use it (or the approach described) to do what you intend. The basic idea is to cancel the change before it is becoming visible (via an OnTextChanging event).
您还可以在CodeProjct上查看FilterTextBox。你可以使用它(或描述的方法)来做你想要的。基本思想是在变化变为可见之前取消变更(通过OnTextChanging事件)。
#3
2
What if you put the validation in OnTextChanged instead of OnKeyPress, but each time it passes validation you save the value to a variable? Then you can revert if the user pastes or types an incorrect string, as well as give some other UI hint that something was invalid (e.g. set a Label's text).
如果您将验证放在OnTextChanged而不是OnKeyPress,但每次通过验证时将值保存到变量,该怎么办?然后,如果用户粘贴或键入不正确的字符串,您可以还原,并提供一些其他UI提示,表明某些内容无效(例如,设置Label的文本)。
#4
1
Why don't you put the check for valid characters in the OnTextChanged event
为什么不在OnTextChanged事件中检查有效字符
and then deal with the Ctrl+C, Ctrl+V in the on key down
然后处理按键上的Ctrl + C,Ctrl + V键
Also you can use the e.ModifierKeys == Keys.Control to test for control keys
您还可以使用e.ModifierKeys == Keys.Control来测试控制键
http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx
#5
0
The solution that I have come up with is to check the keys in the OnKeyDown event and then setting a flag if the keypress should be handled, which is then check in the OnKeyPress event.
我想出的解决方案是检查OnKeyDown事件中的键,然后设置一个标志,如果应该处理keypress,然后检查OnKeyPress事件。
protected override void OnKeyDown(KeyEventArgs e)
{
Keys keyCode = (Keys)e.KeyValue;
base.OnKeyDown(e);
if ((e.Modifiers == Keys.Control) ||
(e.Modifiers == Keys.Control) ||
(keyCode == Keys.Back) ||
(keyCode == Keys.Delete))
{
this._handleKey = true;
}
else
{
// check if the key is valid and set the flag
this._handleKey = Regex.Match(key.ToString(), this._ValidCharExpression).Success;
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (this._handleKey)
{
base.OnKeyPress(e);
this._handleKey = false;
}
else
{
e.Handled = true;
}
}