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

Posted mjsoftware

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();

        

以上是关于WPF TextBox 允许输入数字及字母的工具类的主要内容,如果未能解决你的问题,请参考以下文章

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

wpf 限制textbox只能输入数字及特殊键

WPF文本框只允许输入数字

如何让wpf 中的textbox只能输入整型或浮点型呢??

c#WINFORM中验证在文本框输入的只能是数字和字母,这个怎么做???

wpf textbox允许输入多少