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

            mskMobile2.Focus()

        ElseIf keyAsscii = Keys.Back And Count = 5 Then

            txtContactName.Focus()

        End If

    End Sub

and maskedtextbox is empty with length "5"

If the maskedtextbox is empty then the length is 0, not 5.

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.

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

What is the mask you are using? I'll plug it in and play with it here.

I am using ####-####### as mask.

Hi,

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
            SendKeys.Send("{TAB}")
        End If
    End Sub

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

hi
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

            mtxtLandline.Focus()

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.