| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Muchinfo.WPF.Controls.Textbox
- {
- /// <summary>
- /// BankAccountNumberTextBox.xaml 的交互逻辑
- /// </summary>
- public partial class BankAccountNumberTextBox:UserControl
- {
- public BankAccountNumberTextBox()
- {
- InitializeComponent();
- }
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value);}
- }
- public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BankAccountNumberTextBox),new FrameworkPropertyMetadata(SetTextBoxValue){BindsTwoWayByDefault=true});
- private static void SetTextBoxValue(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as BankAccountNumberTextBox).txtBankAccountNumber.Text = e.NewValue.ToString();
- string NewTxt = (d as BankAccountNumberTextBox).txtBankAccountNumber.Text.Replace(" ", "");
- Char[] char1 = NewTxt.ToCharArray();
- StringBuilder sb = new System.Text.StringBuilder();
- for (int i = 0; i < NewTxt.Length; i++)
- {
- if (i != 0 && i % 4 == 0 && i <= 16)
- {
- sb.Append(" ");
- }
- sb.Append(char1[i]);
- }
- (d as BankAccountNumberTextBox).txtBankAccountNumber.Text = sb.ToString();
- (d as BankAccountNumberTextBox).txtBankAccountNumber.Select((d as BankAccountNumberTextBox).txtBankAccountNumber.Text.Length, 0);
- }
-
- private void txtBankAccountNumber_TextChanged(object sender, TextChangedEventArgs e)
- {
- try
- {
- //屏蔽中文输入和非法字符粘贴输入
- TextBox textBox = sender as TextBox;
- TextChange[] change = new TextChange[e.Changes.Count];
- e.Changes.CopyTo(change, 0);
- int offset = change[0].Offset;
- decimal numb = 0;
- if (change[0].AddedLength > 0)
- {
- if (!decimal.TryParse(textBox.Text.Replace(" ", ""), out numb))
- {
- textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
- textBox.Select(offset, 0);
- }
- Text = txtBankAccountNumber.Text.Replace(" ", ""); ;
- }
- }
- catch (System.Exception ex)
- {
- }
- }
-
- private void txtBankAccountNumber_KeyDown(object sender, KeyEventArgs e)
- {
- TextBox txt = sender as TextBox;
- //屏蔽非法按键
- if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
- {
- e.Handled = false;
- }
- else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
- {
- e.Handled = false;
- }
- else if (e.Key == Key.Tab)
- {
- e.Handled = false;
- }
- else
- {
- e.Handled = true;
- }
- }
- private void txtBankAccountNumber_KeyUp(object sender, KeyEventArgs e)
- {
- TextBox txt = sender as TextBox;
- string NewTxt = txt.Text.Replace(" ", "");
- Char[] char1 = NewTxt.ToCharArray();
- StringBuilder sb = new System.Text.StringBuilder();
- for (int i = 0; i < NewTxt.Length; i++)
- {
- if (i != 0 && i % 4 == 0 && i <= 16)
- {
- sb.Append(" ");
- }
- sb.Append(char1[i]);
- }
- txt.Text = sb.ToString();
- txtBankAccountNumber.Select(txt.Text.Length, 0);
- }
- }
- }
|