WPF 数字框实现

11 篇文章 0 订阅

Xaml的代码:

<Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="198,122,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"  MaxLength="5" BorderBrush="#FF444444" Foreground="#FF3C4661" FontSize="13"
                  DataObject.Pasting="textBox_Pasting"  InputMethod.IsInputMethodEnabled="False"
                  Text="0"
                  />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="0,122,0,0" Name="textBox2" VerticalAlignment="Top" Width="120"  MaxLength="5" BorderBrush="#FF444444" Foreground="#FF3C4661" FontSize="13"
                  DataObject.Pasting="textBox_Pasting"  InputMethod.IsInputMethodEnabled="False"
                  Text="0"
                  />
    </Grid>

 

CS的代码:

  public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
       
            this.AddHandler(TextBox.PreviewKeyDownEvent, new KeyEventHandler(textBox_PreviewKeyDown));
            this.AddHandler(TextBox.PreviewTextInputEvent, new TextCompositionEventHandler(textBox_PreviewTextInput));
            this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(textBox_TextChanged));
            this.AddHandler(TextBox.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(textBox_GotKeyboardFocus));
          
        }

        void textBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var textBox = e.OriginalSource as TextBox;
            if (textBox == null)
            {
                return;
            }
            if (textBox.IsKeyboardFocused)
            {
                textBox.SelectionStart = textBox.Text.Length;
                textBox.ScrollToEnd();
            }
        }

        private void textBox_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!TextBoxHelper.IsNumberic(text))
                {
                    e.CancelCommand();
                }
            }
            else
            {
                e.CancelCommand();
            }
        }

        private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
                e.Handled = true;
        }

        private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

            e.Handled = false;
            if (!TextBoxHelper.IsNumberic(e.Text))
            {
                e.Handled = true;
            }
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = e.OriginalSource as TextBox;
            if (textBox == null)
            {
                return;
            }
            textBox.SetTextValue();
        }
    }

  

 public static class TextBoxHelper
    {
        public static void SetTextValue(this TextBox textBox)
        {
            int testInt = 0;
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                textBox.Text = testInt.ToString();
                return;
            }
            if (textBox.Text.Length > 1)
            {
                textBox.Text = textBox.Text.TrimStart(new char[] { '0' });
            }

            textBox.Tag = textBox.Text;
            textBox.SelectionStart = textBox.Text.Length;
            textBox.ScrollToEnd();
        }

        public static bool IsNumberic(this string strValue)
        {
            if (string.IsNullOrEmpty(strValue))
                return false;
            foreach (char c in strValue)
            {
                if (!char.IsDigit(c))
                {
                    return false;
                }
            }
            return true;
        }
    }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值