WPF TextBox 允许输入数字及字母的工具类

WPF TextBox 允许输入数字及字母的工具类

WPF TextBox 仅允许数字、数字及小数点、字母及数字等进行输入。
本工具默认禁用TextBox获取焦点时的输入法功能,仅能通过键盘输入字符,字符输入受限于正则表达式,使用简单。

工具代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using System.Windows.Input;

namespace 命名空间
{
    /// <summary>
    /// TextBox 输入控制
    /// </summary>
    public class MaskInputTextBoxUtils
    {

        #region 《正则表达式常量》

        /// <summary>
        /// 字母大小写和数字
        /// </summary>
        public const string LETTERS_AND_NUMBERS = @"[^A-Za-z0-9]";
        /// <summary>
        /// 数字和小数点
        /// </summary>
        public const string NUMBERS_AND_POINT = @"[^0-9.]";
        /// <summary>
        /// 纯数字
        /// </summary>
        public const string NUMBERS = @"[^0-9]";

        #endregion


        public static MaskInputTextBoxUtils CreateObject(TextBox TextBoxControl, string RegExp)
        {
            return new MaskInputTextBoxUtils(TextBoxControl, RegExp);
        }

        private MaskInputTextBoxUtils(TextBox textBoxControl, string regExp)
        {
            this.TextBoxControl = textBoxControl;
            this.RegexObj = new Regex(regExp);
        }

        public Regex RegexObj { get; protected set; }

        public TextBox TextBoxControl { get; protected set; }

        public void Build()
        {
            ///关闭输入法
            InputMethod.SetPreferredImeState(TextBoxControl, InputMethodState.Off);
            InputMethod.SetIsInputMethodEnabled(TextBoxControl, false);

            this.TextBoxControl.PreviewTextInput += TextBoxControl_PreviewTextInput;
            this.TextBoxControl.TextChanged += TextBoxControl_TextChanged;
            this.TextBoxControl.Unloaded += TextBoxControl_Unloaded;
        }

        private void TextBoxControl_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.TextBoxControl.PreviewTextInput -= TextBoxControl_PreviewTextInput;
            this.TextBoxControl.TextChanged -= TextBoxControl_TextChanged;
            this.TextBoxControl.Unloaded -= TextBoxControl_Unloaded;
        }

        private void TextBoxControl_TextChanged(object sender, TextChangedEventArgs e)
        {
            //屏蔽非法字符粘贴输入
            TextChange[] change = new TextChange[e.Changes.Count];
            e.Changes.CopyTo(change, 0);
            foreach (TextChange tc in change)
            {
                if (tc.AddedLength <= 0)
                {
                    continue;
                }
                ///找出符合表达式要求的字符输入
                string text = TextBoxControl.Text.Substring(tc.Offset, tc.AddedLength);
                string t = "";
                foreach (char c in text)
                {
                    if (!RegexObj.IsMatch(c.ToString()))
                    {
                        t += c.ToString();
                    }
                }
                TextBoxControl.Text = TextBoxControl.Text.Remove(tc.Offset, tc.AddedLength).Insert(tc.Offset, t);
                TextBoxControl.CaretIndex = tc.Offset + t.Length;
            }
        }

        private void TextBoxControl_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = RegexObj.IsMatch(e.Text);
        }
    }
}

如何使用

     private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ///输入字符控制
            MaskInputTextBoxUtils.CreateObject(this.textBox1, MaskInputTextBoxUtils.LETTERS_AND_NUMBERS).Build();
            MaskInputTextBoxUtils.CreateObject(this.textBox2, MaskInputTextBoxUtils.NUMBERS_AND_POINT).Build();
            MaskInputTextBoxUtils.CreateObject(this.textBox3, MaskInputTextBoxUtils.NUMBERS).Build();

        }
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宏尘极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值