Hi in my country we can phone anyone by only 7 number... Eg 2345897
and sometimes we represent it in this format 234-5897 by a - (negative)..

i want to tell the txtPhoneNumber that after 3 numbers ahve been entered a negative should be place and it should comes automatically ...
i do not want any user to press on the negative frm the keyboard..

I need help 4 this ..

Dani AI

Generated

For a Windows Forms VB.NET app the simplest, most robust fix is to use a MaskedTextBox rather than trying to mutate a TextBox as the user types. As suggested, MaskedTextBox will place the literal hyphen for you, enforce digit input and handle caret/paste/backspace more reliably than a TextChanged hack. ’s TextChanged approach can work, but it commonly causes caret jumps and re-entrancy problems unless you carefully preserve selection and suppress the event while editing. For final validation, ’s idea of using a regular expression (or MaskedTextBox properties) is appropriate.

Quick Win (WinForms)

  • Set the control Mask to a 7-digit pattern with a literal separator so the hyphen appears automatically.
  • Use the control’s MaskCompleted/MaskFull to check completeness and TextMaskFormat to get digits-only when saving.

Example (VB.NET):

' Designer or runtime
MaskedTextBox1.Mask = "000-0000"
MaskedTextBox1.PromptChar = " "c
MaskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals

' Check completeness
If MaskedTextBox1.MaskCompleted Then
    Dim digitsOnly As String = MaskedTextBox1.Text   ' returns just digits
End If

If you cannot use MaskedTextBox (for example in a web form), do client-side masking (input-mask libraries) to prevent odd cursor behavior, and only format/validate on blur or submit. Always store a normalized value (digits only) in the database and validate on the server side as well. For reference on the WinForms control and .NET regex guidance see the MaskedTextBox docs and Regex docs on Microsoft Docs:

Notes and cautions

  • MaskedTextBox handles paste/edit nicely; manual TextChanged insertion needs extra code to preserve SelectionStart and avoid recursive updates.
  • For international/long-term storage, normalize to digits and consider storing numbers in an international format (E.164) rather than display format.

Recommended Answers

All 4 Replies

Hi.
u can use masked text box for ur requirement

You can use regular expression to validate user input, in your case your regular expression'd be ^\d{3}-\d{4}$
So, on your btn validate click event handler you can check if user enters valid phoen number or not

if Regex.IsMatch(txtPhoneNumber.Text, textBox1.Text)
then
MessageBox.Show("Correct")
else
MessageBox.Show("Wrong")

This code if u want to add - after 3 numbers :

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length = 3 Then
            TextBox1.Text = TextBox1.Text + "-"
        End If
    End Sub

This Following code if u want - (negative) will added after user input 7 numbers.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length = 7 Then
            TextBox1.Text = Microsoft.VisualBasic.Left(TextBox1.Text, 3) + "-" + Microsoft.VisualBasic.Mid(TextBox1.Text, 4, TextBox1.Text.Length)
        End If
    End Sub
commented: awesome +1

But this issue can be solved by using masked text box.
if we use no need to write code also

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.