hi guys ! i want to set a focus using backspace keyascii in a maskedtext box so that when i press backspace the focus moves to the other text box or maskedtextbox, but when i press backspace and maskedtextbox is empty with length "5" the focus donot move to the other textbox. Here is my code, Help Please ! Thanks in Advance :)

    Private Sub mskMobile1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles mskMobile1.KeyPress

        Dim Count As Integer
        Dim keyAsscii As Integer
        keyAsscii = Asc(e.KeyChar)

        Count = Len(mskMobile1.Text)

        If keyAsscii = Keys.Enter Then


        ElseIf keyAsscii = Keys.Back And Count = 5 Then


        End If

    End Sub
5 Years
Discussion Span
Last Post by TnTinMN

maskedtextbox can't be empty because it holds " -" value which has length 5.


The placeholders for the mask don't count for the length. If you haven't entered any characters then the length is zero.

Edited by Reverend Jim


ok see these screenshots one by one.

1st ScreenShot:
In this Screen Shot you can see that I have not entered any Character and pressed Enter.

2nd ScreenShot:
In this you can see clearly count has "0" value at its initialization.

3rd ScreenShot
In this i have traced that when i pressed enter key and at that time Maskedtextbox seemed to be empty but when i calculated length it comes out to "5". ScreenShot11

Edited by zaeemabbas20

Attachments ScreenShot2.jpg 349.29 KB ScreenShot3.jpg 348.97 KB


You need the Keydown event from your Maskedtextbox for that.
Here's an example with 5 textboxes:

 Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, TextBox2.KeyDown, _
        TextBox3.KeyDown, TextBox4.KeyDown, TextBox5.KeyDown
        If e.KeyCode = Keys.Back Then
        End If
    End Sub

Edited by Luc001


I have to admit. You were right. Because the user can enter digits at any position, the string can contain embedded blanks. As such you can get the actual number of digits entered by

if isnumeric(keyAscii) Then
    numdigits = 1 + Len(mskMobile1.Text.Replace(" ", "").Replace("-", ""))

which accounts for the digit that has been pressed but not processed.


Try setting the "TextMaskFormat" before retrieving the text.

MaskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals


im approve TnTinMN's answer
you should set textmaskformat prop of maskedtextbox to excludepromtandliterals
this is your solve


thnx guys but the problem is still there, when i press backspace key in maskedtextbox while tracing in keypress event, nothing happens, but when i press any other key keypress event detects it and start tracing.
Now see what i exactly want to do, that when i press backspace key and maskedtextbox is empty means have no numeric or alphabetic value in it then cursor moves in the previous textbox or maskedtextbox, the code below works fine with "Textbox" but not with "Maskedtextbox".

ElseIf keyAsscii = Keys.Back And txtAddress.Text = "" Then


use the keydown event not keypress. You cannot trap backspace on keypress.

Edited by TnTinMN

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.