检测CTRL + V键上preSS [英] Detecting ctrl+v on key press
问题描述
我使用文本框键preSS
事件处理只有选择的输入。基本上文本
允许用户在那里可以计算的输入值。
即。你可以键入(5 * 5) - (10/5)
目前的检查方法,如 Convert.ToChar(*)== e.KeyChar
等等...
目前的时刻它不允许用户复制粘贴值。
反正是有,能检测出 CTRL + V
在键preSS
事件?
更新
我在做什么,此刻
静态的IEnumerable<焦炭> ValidFiancialCharacters
{
得到
{
如果(_validFiancialCharacters == NULL)
{
_validFiancialCharacters =新的名单,其中,烧焦>();
_validFiancialCharacters.Add(Convert.ToChar(0));
_validFiancialCharacters.Add(Convert.ToChar(1));
_validFiancialCharacters.Add(Convert.ToChar(2));
//直到9
_validFiancialCharacters.Add(Convert.ToChar(+));
_validFiancialCharacters.Add(Convert.ToChar( - ));
_validFiancialCharacters.Add(Convert.ToChar(/));
//和一些其他
}
返回_validFiancialCharacters;
}
}
公共静态布尔ValidateInput(主要pressEventArgs E)
{
如果(ValidFiancialCharacters.Any(CHR => CHR == e.KeyChar))
{
e.Handled = FALSE;
}
其他
{
e.Handled =真实;
}
返回e.Handled;
}
而在关键的preSS
私人无效txtRate_Key preSS(对象发件人,重点pressEventArgs E)
{
NumberExtension.ValidateInput(E);
}
如果你需要同时处理,可以使用类似的方法来完成
看一看关键codeS 。你可以检测一下以往的关键是pressed
所以..
创建一个包含所有你需要的
输入列表 公开名单< INT>主要codeS =新的名单,其中,INT>(){8,17,37,39 .....等};
和使用的KeyDown
事件并使用<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.sup$p$psskey$p$pss%28v=vs.110%29.aspx"相对=nofollow> KeyEventArgs.Sup pressKey preSS 财产
私人无效Txt1_KeyDown(对象发件人,KeyEventArgs E)
{
如果(主要codes.Contains(e.KeyValue)||(e.Key code == Keys.V和放大器;&安培; e.Control))
e.Sup pressKey preSS = FALSE;
其他
e.Sup pressKey preSS = TRUE;
}
您可能需要验证复制粘贴值离开事件
,因为用户可以粘贴任何东西!
I am using text box keypress
event to handle only selected inputs. Basically the textbox
allow user to input values where it can be calculated.
i.e. you can type (5*5)- (10/5)
.
The current method check like Convert.ToChar("*")==e.KeyChar
etc...
At the moment it doesn't allow user to copy paste values.
Is there anyway that can detect the ctrl+v
on keypress
event?
Update
What I am doing at the moment
static IEnumerable<char> ValidFiancialCharacters
{
get
{
if(_validFiancialCharacters==null)
{
_validFiancialCharacters = new List<char>();
_validFiancialCharacters.Add(Convert.ToChar("0"));
_validFiancialCharacters.Add(Convert.ToChar("1"));
_validFiancialCharacters.Add(Convert.ToChar("2"));
// till 9 and
_validFiancialCharacters.Add(Convert.ToChar("+"));
_validFiancialCharacters.Add(Convert.ToChar("-"));
_validFiancialCharacters.Add(Convert.ToChar("/"));
//and some other
}
return _validFiancialCharacters;
}
}
public static bool ValidateInput(KeyPressEventArgs e)
{
if (ValidFiancialCharacters.Any(chr => chr == e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
return e.Handled;
}
And in the keypress
private void txtRate_KeyPress(object sender, KeyPressEventArgs e)
{
NumberExtension.ValidateInput(e);
}
If you need to handle both, can be done using the similar approach
Have a look at Key Codes. You can detect what ever the key been pressed
so..
Create a list containing all the inputs you need
public List<int> KeyCodes = new List<int>() { 8, 17, 37, 39.....etc};
and use the KeyDown
event and use KeyEventArgs.SuppressKeyPress property
private void Txt1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyCodes.Contains(e.KeyValue) || (e.KeyCode==Keys.V && e.Control))
e.SuppressKeyPress = false;
else
e.SuppressKeyPress=true;
}
You might need to validate the copy pasted value in leave event
, since user can paste anything!!!
这篇关于检测CTRL + V键上preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!