Home All Groups Group Topic Archive Search About

On the fly formatting of textbox contents

Author
3 May 2006 7:17 PM
cj
I have textbox1 that takes a phone number xxx-xxx-xxxx.  Is it possible
to have it start off looking like    -   -    and when someone types the
number fill in around the -s?

If not how about after they type the 3rd number have the - fill in
automatically etc.

Lastly if that isn't doable then where is the best place to put code to
reformat the number after they are finished entering it.  Perhaps
textbox1_leave doesn't appear to work?

Author
3 May 2006 7:55 PM
cj
I got textbox1_leave working.  I'd still like answers to my first two
options if anyone knows how.

cj wrote:
Show quoteHide quote
> I have textbox1 that takes a phone number xxx-xxx-xxxx.  Is it possible
> to have it start off looking like    -   -    and when someone types the
> number fill in around the -s?
>
> If not how about after they type the 3rd number have the - fill in
> automatically etc.
>
> Lastly if that isn't doable then where is the best place to put code to
> reformat the number after they are finished entering it.  Perhaps
> textbox1_leave doesn't appear to work?
Author
4 May 2006 8:53 AM
Gary Chang[MSFT]
Hi,

Just as MVP Cor suggested, in Visual Studio 2005 you can use the
MaskedEditBox--just set its Mask as 000-000-000.

If you work with VS.NET 2003, I think you can set the textbox's default
text as "000-000-000", then handle that textbox's event as follows:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        If e.KeyChar > "9" Or e.KeyChar < "0" Then
            e.Handled = True
        End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text.Length = 3 Then
            TextBox1.Text += "-"
            TextBox1.Select(TextBox1.Text.Length, 0)
        End If

        If TextBox1.Text.Length = 7 Then
            TextBox1.Text += "-"
            TextBox1.Select(TextBox1.Text.Length, 0)
        End If
End Sub

By the way, in the textbox's TextChanged event handler, you also need to
check and modify the string if the user paste stuff to it.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
4 May 2006 1:35 PM
cj
thanks

Gary Chang[MSFT] wrote:
Show quoteHide quote
> Hi,
>
> Just as MVP Cor suggested, in Visual Studio 2005 you can use the
> MaskedEditBox--just set its Mask as 000-000-000.
>
> If you work with VS.NET 2003, I think you can set the textbox's default
> text as "000-000-000", then handle that textbox's event as follows:
>
> Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
> System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
>
>         If e.KeyChar > "9" Or e.KeyChar < "0" Then
>             e.Handled = True
>         End If
> End Sub
>
> Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles TextBox1.TextChanged
>
>         If TextBox1.Text.Length = 3 Then
>             TextBox1.Text += "-"
>             TextBox1.Select(TextBox1.Text.Length, 0)
>         End If
>
>         If TextBox1.Text.Length = 7 Then
>             TextBox1.Text += "-"
>             TextBox1.Select(TextBox1.Text.Length, 0)
>         End If
> End Sub
>
> By the way, in the textbox's TextChanged event handler, you also need to
> check and modify the string if the user paste stuff to it.
>
> Thanks!
>
> Best regards,
>
> Gary Chang
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
Author
5 May 2006 3:30 AM
Gary Chang[MSFT]
You are welcome!

Good Luck!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.