【发布时间】:2012-04-20 18:50:39
【问题描述】:
当用户按下MaskedTextBox 中的ENTER/RETURN 键时,我正在设置SuppressKeyPress = true,以防止通常发出的恼人的哔哔声。这很好用,但是当我清除我的表单时,MaskedTextBox 不再按预期运行。输入的第一个字符是幻影字符,在输入第二个字符后消失。
例子:
__.___
Set text = "0"
0_.___
User enters text
09.999
User presses ENTER
User presses Save & Next (this clears the form)
Reset text = "0"
0_.___
User enters first 9
09_.___
User enters second 9
0_.9__
如果用户在MaskedTextBox 之外使用 TABS 而不是按 ENTER,这可以正常工作(正确输入文本,没有任何奇怪的移位。)我能找到的唯一区别是我使用的是 SuppressKeyPress 并且Non-Public Members 中的 flagState 不同(当我不 SuppressKeyPress 时为 2052,当我在 SuppressKeyPress 时为 2048。)
有没有办法在不破坏 MaskedTextBox 的情况下防止 BEEP 或在 SuppressKeyPress 之后修复 MaskedTextBox 的方法(我已经尝试了大部分 MaskedTextBox 本身的方法:@ 987654333@、refresh等……)
这里是 MaskedTextBox 的定义和 KeyDown 方法:
//
// aTextBox
//
this.aTextBox.Location = new System.Drawing.Point(130, 65);
this.aTextBox.Mask = "##.###";
this.aTextBox.Name = "aTextBox";
this.aTextBox.Size = new System.Drawing.Size(50, 20);
this.aTextBox.TabIndex = 3;
this.aTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.general_KeyDown);
this.aTextBox.Leave += new System.EventHandler(this.validate);
general_KeyDown 看起来像这样:
private void general_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
SendKeys.Send("{TAB}");
}
}
【问题讨论】:
标签: c# .net winforms keydown maskedtextbox