I’m binding a textbox to a member of a class, and I need to tweak the appearance of the phone number so that it’s easier to read (the user doesn’t want to see values such as “1234567890” or “+01234567890”). So, I’ve got this code:
var bindingPhone = new Binding("Text", platypusInfo, "Phone1", true);
bindingPhone.Format += phoneBinding_Format;
textBoxPhoneNum1.DataBindings.Add(bindingPhone);
...
private void phoneBinding_Format(object sender, ConvertEventArgs e) {
e.Value = ??How can I deal with this??
}
But the phone values, although usually either “NNNNNNNNNN” (such as “1234567890”) or “+NNNNNNNNNNN” (such as “+01234567890”) can also appear in a number of other permutations, such as:
(NN) NNNN NNNN
++NNNNNNNNNNNNN
+NNNNNNNNNNNNN
+NN NNNNNNNNNNN
NNNNNNNNNNNN
Is there anything I can do in phoneBinding_Format() that will make these phone numbers easier to read without breaking them into nonsensical parts, such as “43-4859-4365” instead of “434-859-4365”?
UPDATE
Due to these factors:
1) I’m working on several projects simultaneously and need to get back to another one
2) Our two most common formats comprise the lion’s share of our phone numbers
3) This is just a “nice feature” not a “must-have” feature
…I’ve settled on the following for now, based on a Jon Skeet answer:
private void phoneBinding_Format(object sender, ConvertEventArgs e)
{
const int UK_PHONE_LEN = 9; // +NNNNNNNN
const int US_PHONE_FORMAT_LEN = 10; // NNNNNNNNNN
const int COMMON_INTERNATIONAL_FORMAT_LEN = 12; //+NNNNNNNNNNN
string phone;
string area;
string major;
string minor;
string intl_firstsegment;
string intl_secondsegment;
string intl_thirdsegment;
string intl_fourthsegment;
string intl_fifthsegment;
if (e.Value.ToString().Length == US_PHONE_FORMAT_LEN)
{
phone = e.Value.ToString();
area = phone.Substring(0, 3);
major = phone.Substring(3, 3);
minor = phone.Substring(6);
e.Value = string.Format("{0}-{1}-{2}", area, major, minor);
}
else if ((e.Value.ToString().Length == UK_PHONE_LEN) && (e.Value.ToString()[0] == '+')) {
phone = e.Value.ToString();
intl_firstsegment = phone.Substring(0, 2);
intl_secondsegment = phone.Substring(2, 3);
intl_thirdsegment = phone.Substring(5);
e.Value = string.Format("+{0}-{1}-{2}", intl_firstsegment, intl_secondsegment, intl_thirdsegment);
}
else if ((e.Value.ToString().Length == COMMON_INTERNATIONAL_PHONE_LEN) && (e.Value.ToString()[0] == '+'))
{
phone = e.Value.ToString();
intl_firstsegment = phone.Substring(0, 2);
intl_secondsegment = phone.Substring(2, 2);
intl_thirdsegment = phone.Substring(4, 3);
intl_fourthsegment = phone.Substring(7, 2);
intl_fifthsegment = phone.Substring(9);
e.Value = string.Format("+{0}-{1}-{2}-{3}-{4}", intl_firstsegment, intl_secondsegment, intl_thirdsegment, intl_fourthsegment, intl_fifthsegment);
}
}
BTW, an interesting thing happened on the way to breakpoint nirvana: I originally had these tests (1st character is a plus sign and length is the expected) reversed, and got: System.IndexOutOfRangeException was unhandled by user code
Message=Index was outside the bounds of the array.
Reversing the condition so that length was checked first (which naturally doesn’t fail when length is 0/string is empty) fixed it (since then no attempt is made to access char 0).
Google’s LibPhoneNumber could be just what you need if you want to support international phone numbers in addition to US (and Canadian, which are 100% compatible with US) numbers.
Using it from C#:
http://blog.thekieners.com/2011/06/06/using-googles-libphonenumber-in-microsoft-net-with-c/
C# port
https://bitbucket.org/pmezard/libphonenumber-csharp/wiki/Home