how to format TextBox Like xx-xx-xxxx

mkhurram92

Member
Joined
May 27, 2011
Messages
16
Location
Jeddah(KSA)
Programming Experience
1-3
hello all there

can anyone know how i can store database records in TextBox like this format xx-xx-xxxx
It is just to show them to the users not anything else !!
 
Please explain clearly what you are trying to do

how i can store database records in TextBox like this format xx-xx-xxxx
It is just to show them to the users not anything else !!

If you need a mask for input you can just use a masked textbox? if you want to format data into a textbox you can just use the .ToString() method of a string. For example if you have three variables that you want formatted that way you can use

String.Format("{0}-{1}-{2}", var1, var2, var3)

'if it is a date you can do something like
Now.ToString("MM-dd-yyyy")


otherwise, please clarify, your question is very confusing. :confused:
 
otherwise, please clarify, your question is very confusing. :confused:
i m sorry to not clarify it well... i want if i write 55555 in TextBox It should automatically converted to 55-555 format when i leave this TextBox Control and move to other TextBox... Nothing more than This... !!!
 
I would suggest using a Masked Textbox, just change the mask to 00-000, there are also some presets like for phone numbers and zip codes that you can use as well. If the input is going to be characters as well, you can use AA-AAA for the mask to allow all alphabet and numbers.

Otherwise another suggestion is to use the leave event of a textbox, although you would have to write code to catch for things like how long the text is and so forth. The Masked Textbox control is probably the best idea for this situation i think.

Anyways if you use a textbox and use the leave event you can do something like this
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        'you can do it this way too if you are writing a method that handles more than one textbox
        'Dim thisTextBox As TextBox = DirectCast(sender, TextBox)

        'thisTextBox.Text = thisTextBox.Text.Insert(2, "-")
        'otherwise in this case you can just do
        TextBox1.Text = TextBox1.Text.Insert(2, "-")
    End Sub
 
Back
Top