博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
customTextbox
阅读量:6289 次
发布时间:2019-06-22

本文共 5446 字,大约阅读时间需要 18 分钟。

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

 

转载于:https://www.cnblogs.com/FaDeKongJian/p/3149185.html

你可能感兴趣的文章
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>
【Docker学习笔记(四)】通过Nginx镜像快速搭建静态网站
查看>>
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
<转>云主机配置OpenStack使用spice的方法
查看>>
java jvm GC 各个区内存参数设置
查看>>
[使用帮助] PHPCMS V9内容模块PC标签调用说明
查看>>
关于FreeBSD的CVSROOT的配置
查看>>
基于RBAC权限管理
查看>>
数学公式的英语读法
查看>>
留德十年
查看>>
迷人的卡耐基说话术
查看>>
PHP导出table为xls出现乱码解决方法
查看>>
PHP问题 —— 丢失SESSION
查看>>
Java中Object类的equals()和hashCode()方法深入解析
查看>>
数据库
查看>>
Vue------第二天(计算属性、侦听器、绑定Class、绑定Style)
查看>>
dojo.mixin(混合进)、dojo.extend、dojo.declare
查看>>