8 Replies - 5567 Views - Last Post: 04 May 2011 - 06:15 AM Rate Topic: -----

#1 Program Child  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 33
  • Joined: 03-December 08

Masked TextBox adding extra character

Posted 31 October 2010 - 03:41 PM

I'm working on a project that employs a masked text box with a mask ####. When I run the code it pulls the value from the textbox then clears it. When I try to type in it again the first character is added before the mask. (ex. 1#### instead of ####) I've figured out that pressing backspace before trying to enter numbers again resolves this issue. Is there a way to send the backspace function rather than the chrs(8) like
SendKeys.Send(Keys.Back)
does?

Is This A Good Question/Topic? 0
  • +

Replies To: Masked TextBox adding extra character

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Masked TextBox adding extra character

Posted 31 October 2010 - 06:43 PM

How is the mask property defined?
Was This Post Helpful? 0
  • +
  • -

#3 Program Child  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 33
  • Joined: 03-December 08

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 09:47 AM

View Postn8wxs, on 31 October 2010 - 05:43 PM, said:

How is the mask property defined?

The Mask property is set in the IDE as #### or 4 Numbers.
Was This Post Helpful? 0
  • +
  • -

#4 Program Child  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 33
  • Joined: 03-December 08

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 10:01 AM

View PostProgram Child, on 01 November 2010 - 08:47 AM, said:

View Postn8wxs, on 31 October 2010 - 05:43 PM, said:

How is the mask property defined?

The Mask property is set in the IDE as #### or 4 Numbers.


Sorry about the double post, it wouldn't allow me to edit my last post.

I created a quick application to mimic the error. It contains one masked textbox with a mask of #### and one button.
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim I1 As String
        I1 = InputBox("Please enter some random text", "Random Text", MaskedTextBox1.Text)
        MaskedTextBox1.Clear()
        MaskedTextBox1.Focus()
    End Sub

    Private Sub MaskedTextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MaskedTextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Button1_Click(Nothing, Nothing)
        End If
    End Sub
End Class



To me it looks like the Key Event is causing the error somehow.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 10:28 AM

I can not make your problem show up either with my default code or your test code.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 10:38 AM

This appears to be a bug. If the focus is lost and then focus is given again, it is allowing an additional character each time. You might have to do something with the MaskFull property to inhibit any other keys. I'll see what I can come up with. If you come up with something, please post it.

View Postn8wxs, on 01 November 2010 - 11:28 AM, said:

I can not make your problem show up either with my default code or your test code.



, in testing this, it appears to be something with using Enter. I've tried e.SuppressKeyPress. If you press the enter key after entering the mask, then hit enter on the inputbox, you can put an extra keystroke into the MTB. It took me removing the MaskedTextBox1.Clear() to find what was happening, now just what to do to fix it.

Here's my test code, try entering a number then press enter, then press enter on the input box and then type a number.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim I1 As String
        I1 = InputBox("Please enter some random text", "Random Text", MaskedTextBox1.Text)
        MaskedTextBox1.Focus()
    End Sub


    Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MaskedTextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            Button1.PerformClick()
        End If
    End Sub

This post has been edited by CharlieMay: 01 November 2010 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 10:51 AM

Well, this seems to work. Don't ask me why, but I decided to use Keypress instead

Try this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim I1 As String
        I1 = InputBox("Please enter some random text", "Random Text", MaskedTextBox1.Text)
        MaskedTextBox1.Clear()
        MaskedTextBox1.Focus()
    End Sub

    Private Sub MaskedTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
        If e.KeyChar = ChrW(13) Then
            e.Handled = True
            Button1.PerformClick()
        End If
    End Sub

Was This Post Helpful? 1
  • +
  • -

#8 Program Child  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 33
  • Joined: 03-December 08

Re: Masked TextBox adding extra character

Posted 01 November 2010 - 11:56 AM

View PostCharlieMay, on 01 November 2010 - 09:51 AM, said:

Well, this seems to work. Don't ask me why, but I decided to use Keypress instead

Try this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim I1 As String
        I1 = InputBox("Please enter some random text", "Random Text", MaskedTextBox1.Text)
        MaskedTextBox1.Clear()
        MaskedTextBox1.Focus()
    End Sub

    Private Sub MaskedTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
        If e.KeyChar = ChrW(13) Then
            e.Handled = True
            Button1.PerformClick()
        End If
    End Sub


That worked. I'm still curious as to why it was doing that, but for now I will just acknowledge that KeyPress works and leave it at that. :)
Was This Post Helpful? 0
  • +
  • -

#9 Incredible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-May 11

Re: Masked TextBox adding extra character

Posted 04 May 2011 - 06:15 AM

View PostProgram Child, on 31 October 2010 - 03:41 PM, said:

I'm working on a project that employs a masked text box with a mask ####. When I run the code it pulls the value from the textbox then clears it. When I try to type in it again the first character is added before the mask. (ex. 1#### instead of ####) I've figured out that pressing backspace before trying to enter numbers again resolves this issue. Is there a way to send the backspace function rather than the chrs(8) like
SendKeys.Send(Keys.Back)
does?


I was satisfied because at least I was able to identify that the problem can be resolved by pressing Backspace key.
I tried the following code and it really works:
Private Sub MyForm_KeyDown(Byval sender As Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If UCase(ActiveControl.Name.ToString.Trim) = "TXT_DATE" Then
If e.KeyCode = Keys.Escape Then
e.SuppressKeyPress = True
SendKeys.Send("{BACKSPACE}")
End If
End If
End Sub
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1