[英]After making a hex mask CTRL+C and CTRL+V is not allowed
I have a small problem. 我有一个小问题。 After making a hex mask, I can not copy/paste with Ctrl + C / V . 制作完十六进制蒙版后,无法使用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 . 但是我希望能够只按Ctrl + V。
If I delete the hex mask, Ctrl + C / V works fine. 如果删除十六进制蒙版,则Ctrl + C / V可以正常工作。
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? 有人可以给我一些提示吗?
You need to catch these keystrokes with a KeyDown event handler, not KeyPressed. 您需要使用KeyDown事件处理程序(而不是KeyPressed)来捕获这些击键。 KeyPressed is only raised for typing keys. 仅引发KeyPressed以便键入键。
A MaskedTextBox is not ideal here, you can also do it with a regular TextBox. MaskedTextBox在这里不是理想的,您也可以使用常规TextBox来实现。 Use the Validating event to format the number and check for range. 使用Validating事件来格式化数字并检查范围。 For example: 例如:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
bool ok = e.KeyChar == 8; // Backspace
if ("0123456789ABCDEF".Contains(char.ToUpper(e.KeyChar))) ok = true;
if (!ok) e.Handled = true;
}
private void textBox1_Validating(object sender, CancelEventArgs e) {
int value;
if (textBox1.Text.Length > 0) {
if (!int.TryParse(this.textBox1.Text, System.Globalization.NumberStyles.HexNumber, null, out value)) {
this.textBox1.SelectAll();
e.Cancel = true;
}
else {
textBox1.Text = value.ToString("X8");
}
}
}
You had: 你有过:
if (e.KeyChar == (char)03)
{
// is <CTRL + V>
e.Handled = true;
}*/
According to Cisco the value for Ctrl + V is 22
so you should have: 根据思科的说法, Ctrl + V的值为22
因此您应该具有:
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$") && e.KeyChar != 22)
{
e.Handled = true;
}
The MaskedTextBox
might block the Ctrl + V call (otherwise you could easily circumvent the mask). MaskedTextBox
可能会阻止Ctrl + V调用(否则,您可以轻松绕过掩码)。 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. 就个人而言,我不会使用带遮罩的文本框,而是单独验证输入,并在输入出现问题时警告用户。 The MaskedTextBox
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. MaskedTextBox
具有普遍使用的缺点,因为它不是用户习惯的常规组件,因此更习惯于告诉用户输入错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.