• customTextbox


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Input;
    
    namespace MyWPFCustomControls
    {
        public class CustomTextBox : TextBox
        {
            public CustomTextBox()
            {
    
            }
    
    
    
            static CustomTextBox()
            {
    
            }
            public CustomTextBoxType CustomTextBoxType
            {
                get { return (CustomTextBoxType)GetValue(CustomTextBoxTypeProperty); }
                set { SetValue(CustomTextBoxTypeProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for CustomTextBoxType.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CustomTextBoxTypeProperty =
                DependencyProperty.Register("CustomTextBoxType", typeof(CustomTextBoxType), typeof(CustomTextBox), new UIPropertyMetadata(CustomTextBoxType.None, new PropertyChangedCallback(OnCustomTextBoxTypeChanged)));
    
    
            private static void OnCustomTextBoxTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
            {
                var tb = obj as CustomTextBox;
    
            }
    
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
            }
            protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
            {
                base.OnPreviewTextInput(e);
            }
            protected override void OnTextChanged(TextChangedEventArgs e)
            {
                base.OnTextChanged(e);
                //屏蔽中文输入和非法字符粘贴输入
    
                TextChange[] change = new TextChange[e.Changes.Count];
                e.Changes.CopyTo(change, 0);
    
                int offset = change[0].Offset;
                if (change[0].AddedLength > 0)
                {
                    switch (this.CustomTextBoxType)
                    {
                        case CustomTextBoxType.None:
                            break;
                        case CustomTextBoxType.Integer:
                            NonIntergerHandle(change, offset);
                            break;
                        case CustomTextBoxType.Decimal:
                            NonDecimalHandle(change, offset);
                            break;
                        case CustomTextBoxType.Alphabet:
                            break;
                        case CustomTextBoxType.Alphanumeric:
                            break;
                        case CustomTextBoxType.Currency:
                            break;
                        default:
                            break;
                    }
    
                }
            }
    
            private void NonDecimalHandle(TextChange[] change, int offset)
            {
                Decimal num = 0;
                if (!Decimal.TryParse(this.Text, out num))
                {
                    this.Text = this.Text.Remove(offset, change[0].AddedLength);
                    this.Select(offset, 0);
                }
            }
    
            private void NonIntergerHandle(TextChange[] change, int offset)
            {          
                long num = 0;
                if (!long.TryParse(this.Text, out num))
                {
                    this.Text = this.Text.Remove(offset, change[0].AddedLength);
                    this.Select(offset, 0);
                }
            }
    
            protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
            {
                base.OnPreviewKeyDown(e);
                switch (this.CustomTextBoxType)
                {
                    case CustomTextBoxType.None:
                        break;
                    case CustomTextBoxType.Integer:
                        IntegerOperation(e);
                        break;
                    case CustomTextBoxType.Decimal:
                        DecimalOperation(e);
                        break;
                    case CustomTextBoxType.Alphabet:
                        break;
                    case CustomTextBoxType.Alphanumeric:
                        break;
                    case CustomTextBoxType.Currency:
    
                        break;
                    default:
                        break;
                }
    
    
            }
    
            private void DecimalOperation(System.Windows.Input.KeyEventArgs e)
            {
                if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal)
                {
    
                    if (this.Text.Contains(".") && e.Key == Key.Decimal)
                    {
                        e.Handled = true;
                        return;
                    }
                    e.Handled = false;
                }
                else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
                {
                    if (this.Text.Contains(".") && e.Key == Key.OemPeriod)
                    {
                        e.Handled = true;
                        return;
                    }
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
    
            private void IntegerOperation(System.Windows.Input.KeyEventArgs e)
            {           
                if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
                {
                    e.Handled = false;
                }
                else if ((e.Key >= Key.D0 && e.Key <= Key.D9) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }
    
    
    
        public enum CustomTextBoxType
        {
            None,
            Integer,
            Decimal,
            Alphabet,
            Alphanumeric,
            Currency
        }
    }
    View Code
  • 相关阅读:
    判断是否可以点击
    窗口截图
    设置等待操作
    时间控件处理
    eclipse小技巧
    Angular 学习1
    MVC 中引用Angularjs
    Bootstrap 侧边栏 导航栏
    C# 直接使用sql语句对数据库操作 (cmd.ExecuteNonQuery)
    sql 常用的语句(sql 创建表结构 修改列 清空表)
  • 原文地址:https://www.cnblogs.com/FaDeKongJian/p/3149185.html
Copyright © 2020-2023  润新知