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

发布时间 2023-11-15 13:58:48作者: 咖啡加点盐
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();

        }