I need some help on formating a textbox, I would like to do the following, if a user enters 9998887777 to on the leave event to format the number to this (999) 888-7777,
Thanks
I need some help on formating a textbox, I would like to do the following, if a user enters 9998887777 to on the leave event to format the number to this (999) 888-7777,
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
JUlius46 reputationPosted Jul 17, 2008, 8:02 AM EDT
Thank you so much Alan.
Julius
AlanRank #669 · 3.6K reputationPosted Jul 17, 2008, 7:28 AM EDT
This should do it:
private
void textBox1_Leave(object sender, EventArgs e){
ulong num; if (textBox1.Text.Length == 10 && ulong.TryParse(textBox1.Text, out num)){
string pn = textBox1.Text;textBox1.Text =
String.Format("({0}) {1}-{2}", pn.Substring(0,3), pn.Substring(3,3), pn.Substring(6));}
else{
MessageBox.Show("Invalid phone number, please change");textBox1.Focus();
}
}
JUlius46 reputationPosted Jul 17, 2008, 7:50 AM EDT
I am sorry Alan, my mistake; I am working in VB2005, Thanks,
Julius
AlanRank #669 · 3.6K reputationPosted Jul 17, 2008, 7:58 AM EDT
No problem, Julius:
Private
Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave Dim num As ULong If TextBox1.Text.Length = 10 AndAlso ULong.TryParse(TextBox1.Text, num) Then Dim pn As String = TextBox1.TextTextBox1.Text =
String.Format("({0}) {1}-{2}", pn.Substring(0, 3), pn.Substring(3, 3), pn.Substring(6)) ElseMessageBox.Show(
"Invalid phone number, please change")TextBox1.Focus()
End If End SubSatish KathiRank #8,369 · 223 reputationPosted Jul 17, 2008, 8:25 AM EDT
JUlius46 reputationPosted Jul 17, 2008, 11:32 AM EDT
Hello Satish:
I have :-), the problem that I had is that the pnone number is a required field when saving the record, and I have added the following to the save event:
If TxtPhone.Text = "" Then
Msgbox "The Phone number is required"
Exit Sub
End if
When I am testing to see if would allow to save , it does save the record, because the Masked of the field is ( ) __-___, it thiks that is something in the field already.
Satish KathiRank #8,369 · 223 reputationPosted Jul 17, 2008, 11:48 AM EDT
Set the "TextMaskFormat" propety of the Masked Text Box to "ExcludePromptAndLiterals"
JUlius46 reputationPosted Jul 17, 2008, 12:38 PM EDT
Thulani Mathenjwa94 reputationPosted Mar 24, 2009, 2:33 AM EDT
{
string d = string.Format("({0}) {1}-{2}", phoneN.Substring(0, 3), phoneN.Substring(3, 3), phoneN.Substring(6));
return d;
}