Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one textBox to add Phone Number to listBox. Listbox value will be SAVED in XML file. for Masking phone Number Im Using Following Code :

I need like this : user enter phone number -> 9876543210 and click ADD button Number will be displayed in ListBox like this format-> XXXXXXX210.
after that user click on SUBMIT button listBox items(Original value 9876543210) will be Saved in xml file.

Thanks,

What I have tried:

string Phone = this.Textbox1.Text;
       string MaskedPhone = string.Format("XXXXXXX{0}", Phone.Trim().Substring(7, 3));
       ListBox1.Items.Add(Country_Code + "-" + MaskedPhone);

  this code masked Xml value too, I need Only ListBox Value should be Masked, Xml value should be as it is.

How can I do it in C# ??
Posted
Updated 4-Feb-20 10:16am

You can either create your own object type to add to the ListBox.ObjectCollection Class (System.Windows.Forms) | Microsoft Docs[^], or use custom drawing to display the number in the format you want.
 
Share this answer
 
Comments
Maciej Los 4-Feb-20 16:16pm    
5ed!
In addition to solution #1 by RichardMacCutchan, you should create your custom class, for example:

C#
public class MaskedPhone
{
	private readonly string countrycode = string.Empty;
	private readonly string phoneno = string.Empty;
	
	public MaskedPhone(string _countrycode, string _phoneno)
	{
		countrycode = _countrycode;
		phoneno = _phoneno;
	}


	public string CountryCode => countrycode;
	public string PhoneNo => phoneno;
	
	public override string ToString()
	{
		return string.Format("{0}-XXXXXXX{1}", countrycode, phoneno.Substring(7, 3));
	}
}


Note: Use proper data types! I've used string data type.

Usage:
C#
List<MaskedPhone> mps = new List<MaskedPhone>()
    {
        new MaskedPhone("+48", "1234567890"),
        new MaskedPhone("+46", "0123456789"),
        new MaskedPhone("+42", "6549873210"),
    };
foreach(MaskedPhone mp in mps)
{
    Console.WriteLine($"{mp.ToString()} => {mp.CountryCode}-{mp.PhoneNo}");
}
 
Share this answer
 
Comments
Richard MacCutchan 4-Feb-20 17:39pm    
+5 for a complete answer.
Maciej Los 5-Feb-20 0:32am    
Thank you very much, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900