Advertisement
FrayxRulez

PhoneTextBox

Aug 19th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1.     public class PhoneTextBox : TextBox
  2.     {
  3.         public string CountryCode
  4.         {
  5.             get { return (string)GetValue(CountryCodeProperty); }
  6.             set { SetValue(CountryCodeProperty, value); }
  7.         }
  8.  
  9.         public static readonly DependencyProperty CountryCodeProperty =
  10.             DependencyProperty.Register("CountryCode", typeof(string), typeof(PhoneTextBox), new PropertyMetadata(null, OnCountryCodeChanged));
  11.  
  12.         private static void OnCountryCodeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  13.         {
  14.             var sender = (PhoneTextBox)d;
  15.             var newValue = (string)e.NewValue;
  16.             var oldValue = (string)e.OldValue;
  17.  
  18.             if (newValue != oldValue)
  19.             {
  20.                 sender.Formatter = PhoneNumberUtil.GetInstance().GetAsYouTypeFormatter(newValue);
  21.                 sender.Text = sender.GetFormatted();
  22.                 sender.SelectionStart = sender.Text.Length;
  23.             }
  24.         }
  25.  
  26.         private AsYouTypeFormatter Formatter;
  27.  
  28.         protected override void OnKeyDown(KeyRoutedEventArgs e)
  29.         {
  30.             if (Formatter != null)
  31.             {
  32.                 if (IsValidKey(e.Key))
  33.                 {
  34.                     Text = Text.Insert(SelectionStart, ValueFromKey(e.Key));
  35.                     Text = GetFormatted();
  36.                     SelectionStart = Text.Length;
  37.                 }
  38.                 else if (e.Key == VirtualKey.Back)
  39.                 {
  40.                     if (SelectionStart > 0)
  41.                     {
  42.                         Text = Text.Remove(SelectionStart - 1, 1);
  43.                         Text = GetFormatted();
  44.                         SelectionStart = Text.Length;
  45.                     }
  46.                 }
  47.             }
  48.  
  49.             e.Handled = true;
  50.  
  51.             base.OnKeyDown(e);
  52.         }
  53.  
  54.         private string GetFormatted()
  55.         {
  56.             Formatter.Clear();
  57.  
  58.             var text = Regex.Replace(Text, "[^0-9]", "");
  59.             var result = string.Empty;
  60.             for (int i = 0; i < text.Length; i++)
  61.             {
  62.                 result = Formatter.InputDigit(text[i]);
  63.             }
  64.  
  65.             return result;
  66.         }
  67.  
  68.         private bool IsValidKey(VirtualKey key)
  69.         {
  70.             var name = Enum.GetName(typeof(VirtualKey), key);
  71.             if (name.StartsWith("Number"))
  72.             {
  73.                 return true;
  74.             }
  75.  
  76.             return false;
  77.         }
  78.  
  79.         private string ValueFromKey(VirtualKey key)
  80.         {
  81.             var name = Enum.GetName(typeof(VirtualKey), key);
  82.             var value = Regex.Match(name, "([0-9])");
  83.  
  84.             return value.Value;
  85.         }
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement