Formatting textbox

Asked

Viewed 71 times

0

Hello, I’m having a formatting problem in the text box which is as follows I am using the following code to format the text box according to the phone number

If Len(numtel) = 11 Then CaixaTxt.Text = Format(numtel, "(##) ##### ####")
    If Len(numtel) = 10 Then CaixaTxt.Text = Format(numtel, "(##) #### ####")
    If Len(numtel) = 9 Then CaixaTxt.Text = Format(numtel, "( ) ##### ####")
    If Len(numtel) = 8 Then CaixaTxt.Text = Format(numtel, "( ) #### ####")

however the text box instead of being formatted with the number and format, is receiving the text: Ex: Len = 8 the box is "( ) #######" with the space of the quotation marks and # instead of numbers... Someone knows what I’m doing wrong?

1 answer

0


The problem will most likely be that the variable numtel be the type String, which prevents formatting from being applied.

The solution is to convert to whole and then make the conversion, more or less in this way:

If IsNumeric(numtel) Then
    Dim intNumtel As Integer = Convert.ToInt32(numtel)
    Dim strNumtel As String

    Select Case Len(numtel)
        Case 11 : strNumtel = Format(intNumtel, "(##) ##### ####")
        Case 10 : strNumtel = Format(intNumtel, "(##) #### ####")
        Case 9 : strNumtel = Format(intNumtel, "( ) ##### ####")
        Case 8 : strNumtel = Format(intNumtel, "( ) #### ####")
    End Select

    CaixaTxt.Text = strNumtel
End If
  • I did not understand the negative vote... by voting negative they should explain why, so that those who put the solution understand what is wrong and change what is necessary to help the user to find one that is valid!

  • Thank you very much! Solved my problem :D As for the negative vote, I also did not understand... The solution worked perfectly Thank you for your time!

  • You are welcome! Take advantage and give an UP on the answer :)

  • I gave, but as I have no more than 15 pts of reputation, only account in the system and does not mark the vote... I do not understand why this, but okay! it was very helpful anyway

Browser other questions tagged

You are not signed in. Login or sign up in order to post.