Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to display Phone Numbers in C#(Win Forms)?
 

How to display Phone Numbers in C#(Win Forms)?

Hi All,

I have 10 or 12 digit Phone number like 1111111111. While displaying it should show as 111-111-1111. How to do it using C#(Win application)?

Thanks in advance..

Ganapatisb  Tuesday, September 29, 2009 5:11 AM
You can use this method.

        static string PhoneNumberFormat(string phoneNumber)
        {
            if (phoneNumber.Length == 10)
            {
                return String.Format("{0}-{1}-{2}", new string[] { phoneNumber.Substring(0, 3), phoneNumber.Substring(3, 3), phoneNumber.Substring(6) });
            }
            else if (phoneNumber.Length == 12)
            {
                return String.Format("{0}-{1}-{2}-{3}", new string[] { phoneNumber.Substring(0, 2), phoneNumber.Substring(2, 3), phoneNumber.Substring(5,3),phoneNumber.Substring(8) });
            }
            else
            {
                return phoneNumber;
            }
        }
Tamer Oz  Tuesday, September 29, 2009 5:28 AM
if you want to accept the number from the user in textboxthen use this in the key down event of the text box
 if(textBox1.Text.Length<12 && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 )|| e.KeyCode==Keys.Back || e.KeyCode==Keys.Delete || e.KeyCode==Keys.Tab))
 {
	 if((textBox1.Text.Length==3||textBox1.Text.Length==7) && !(e.KeyCode==Keys.Back || e->KeyCode==Keys.Delete))
	 {
		 textBox1.AppendText("-");
	 }
 }
 else
 {
	 e.SuppressKeyPress=true;
 }


or just for converting string use


phoneNumber=phoneNumber.Insert(3,"-").Insert(7,"-")
Bharath kumar Y.S  Tuesday, September 29, 2009 5:46 AM
You can use this method.

        static string PhoneNumberFormat(string phoneNumber)
        {
            if (phoneNumber.Length == 10)
            {
                return String.Format("{0}-{1}-{2}", new string[] { phoneNumber.Substring(0, 3), phoneNumber.Substring(3, 3), phoneNumber.Substring(6) });
            }
            else if (phoneNumber.Length == 12)
            {
                return String.Format("{0}-{1}-{2}-{3}", new string[] { phoneNumber.Substring(0, 2), phoneNumber.Substring(2, 3), phoneNumber.Substring(5,3),phoneNumber.Substring(8) });
            }
            else
            {
                return phoneNumber;
            }
        }
Tamer Oz  Tuesday, September 29, 2009 5:28 AM
if you want to accept the number from the user in textboxthen use this in the key down event of the text box
 if(textBox1.Text.Length<12 && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 )|| e.KeyCode==Keys.Back || e.KeyCode==Keys.Delete || e.KeyCode==Keys.Tab))
 {
	 if((textBox1.Text.Length==3||textBox1.Text.Length==7) && !(e.KeyCode==Keys.Back || e->KeyCode==Keys.Delete))
	 {
		 textBox1.AppendText("-");
	 }
 }
 else
 {
	 e.SuppressKeyPress=true;
 }


or just for converting string use


phoneNumber=phoneNumber.Insert(3,"-").Insert(7,"-")
Bharath kumar Y.S  Tuesday, September 29, 2009 5:46 AM

You can use google to search for other answers

Custom Search

More Threads

• User control doesnt appear in the win app
• Designer Keeps Resizing Controls On Build
• Form Controls Have Disappeared
• Design Time Binding approach for components
• Custom control newbie needs help.
• What is the maximum width of a button ?
• the datagrid columns backcolor did not take effect c# 2005
• Code Snippet: Cleaning Rich Text Files
• Network Browser Control Like Explorer
• Custom ToolStripRenderer - Display at Design Time?