简体   繁体   English

VB 掩码文本框丢失数字

[英]VB Masked Textbox Loses Digit

I have a masked textbox in VB.NET to input a time value.我在 VB.NET 中有一个屏蔽文本框来输入时间值。 I've tried the masks 90:00, 00:00, and ##:##.我试过 90:00、00:00 和 ##:## 的面具。 The first time a value is entered in the box, it inputs fine.第一次在框中输入值时,输入正常。 I later clear the text with我后来清除了文字

mskTime.Text = ""

I've also tried我也试过

mskTime.Clear()
mskTime.ResetText()

The issue is after the text is cleared and a new time is entered, the first character typed is deleted.问题是在清除文本并输入新时间后,键入的第一个字符被删除。 More precisely, when I add the time 12:34, I type the 1, it appears in the first character slot.更准确地说,当我添加时间 12:34 时,我输入 1,它出现在第一个字符槽中。 I then press 2, and the 1 disappears and the 2 appears in the second character slot.然后我按 2,1 消失,2 出现在第二个字符槽中。 The character does not disappear when you go to type it again to fix it.当您再次键入以修复它时,该字符不会消失。

Has anyone seen this issue or know why the first character disappears?有没有人看到这个问题或知道为什么第一个字符消失?

Problem found:发现问题:

I had a KeyDown function to handle the user pressing enter in the time field:我有一个 KeyDown 函数来处理用户在时间字段中按下 Enter 键:

    If e.KeyCode = Keys.Enter Then
        Schedule()
        e.SuppressKeyPress = True
    End If

Commenting out注释掉

e.SuppressKeyPress = True 

solves the issue.解决了这个问题。 I originally used that to prevent Windows from making a ding sound each time enter was pressed.我最初使用它是为了防止 Windows 在每次按下 Enter 时发出叮当声。

I had the exact same problem in C#.我在 C# 中遇到了完全相同的问题。 The input worked fine until the MaskedTextBox was cleared programmatically.在以编程方式清除MaskedTextBox之前,输入工作正常。 After that the first character entered in one of the MaskedTextBoxes was deleted after a second character has been entered.之后,在输入第二个字符后,删除在其中一个MaskedTextBoxes输入的第一个字符。 Turned out that the problem lied within the KeyDown event.结果发现问题出在KeyDown事件中。 I was waiting for the Enter -Key there, did some stuff and cleared the TextBoxes .我在那里等待Enter -Key,做了一些事情并清除了TextBoxes After that, the problem occured.之后,问题出现了。

I solved it now by replacing the KeyDown -Event with a KeyPress -Event.我现在通过用KeyPress -Event 替换KeyDown -Event 来解决它。

Here is what I ended up using to catch the Enter Key and move to the next field and not screw up the masked textbox when I come back to it.这是我最终用来捕捉 Enter 键并移动到下一个字段的方法,当我回到它时不会搞砸蒙版的文本框。 Douglas道格拉斯

    private void TxtLocID_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            SendKeys.Send("{Tab}");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM