制作十六进制掩码后,不允许使用 CTRL+C 和 CTRL+V
我有一个小问题。制作十六进制掩码后,我无法使用 Ctrl+C/V 复制/粘贴。如果我右键单击文本框,我可以粘贴。但我希望能够只按 Ctrl+V。
如果我删除十六进制掩码, Ctrl+C/V 工作正常。
这是一些代码:
private void maskedTextBox1(Object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// this will allow a-f, A-F, 0-9, ","
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$"))
{
e.Handled = true;
}
// if keychar == 13this ill allow <ENTER>
if (e.KeyChar == (char)13)
{
button1_Click(sender, e);
}
// I thought I could fix it with the lines below but it doesnt work
/* if (e.KeyChar == (char)22)
{
// <CTRL + C>
e.Handled = true;
}
if (e.KeyChar == (char)03)
{
// is <CTRL + V>
e.Handled = true;
}*/
//MessageBox.Show(((int)e.KeyChar).ToString());
}
有人可以给我一些提示吗?
I have a small problem. After making a hex mask, I can not copy/paste with Ctrl+C/V. If I right click in the textbox I can paste. But I would like to be able to just press Ctrl+V.
If I delete the hex mask, Ctrl+C/V works fine.
Here is a bit of the code:
private void maskedTextBox1(Object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// this will allow a-f, A-F, 0-9, ","
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$"))
{
e.Handled = true;
}
// if keychar == 13this ill allow <ENTER>
if (e.KeyChar == (char)13)
{
button1_Click(sender, e);
}
// I thought I could fix it with the lines below but it doesnt work
/* if (e.KeyChar == (char)22)
{
// <CTRL + C>
e.Handled = true;
}
if (e.KeyChar == (char)03)
{
// is <CTRL + V>
e.Handled = true;
}*/
//MessageBox.Show(((int)e.KeyChar).ToString());
}
Could someone give me some hints, Please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用 KeyDown 事件处理程序而不是 KeyPressed 来捕获这些击键。 KeyPressed 仅在键入按键时才会引发。
MaskedTextBox 在这里并不理想,您也可以使用常规 TextBox 来实现。使用验证事件来格式化数字并检查范围。例如:
You need to catch these keystrokes with a KeyDown event handler, not KeyPressed. KeyPressed is only raised for typing keys.
A MaskedTextBox is not ideal here, you can also do it with a regular TextBox. Use the Validating event to format the number and check for range. For example:
您有:
根据 思科
Ctrl+V 的值为
22
所以你应该有:You had:
According to Cisco
the value for Ctrl+V is
22
so you should have:MaskedTextBox
可能会阻止 Ctrl+V 调用(否则您可以轻松绕过掩码)。就我个人而言,我不会使用屏蔽文本框,而是单独验证输入,并在输入出现问题时提醒用户。MaskedTextBox
在一般使用中存在缺点,因为它不是用户习惯的普通组件,用户更习惯被告知输入错误。The
MaskedTextBox
might block the Ctrl+V call (otherwise you could easily circumvent the mask). Personally, I wouldn't use a masked textbox but validate the input seperately, and alert the user if there is a problem with the input. TheMaskedTextBox
has drawbacks in general use, as it isn't a normal component the user is used to, a user is more used to being told that an input was wrong.