限制较为严格,比如整数或小数,第一位输入“0”,则后面不能输入,不会出现“001”“0001.11”等;
输入页码严格按照“3-7,9,11-13”,样式,不会出现误输入,取得结果也会按顺序、去重输出到list里面。
可以设置:
All = 0, // 任意
Integer = 1, // 整数
Decimal = 2, // 小数
Zh = 3, // 中文
En = 4, // 英文
NoZh = 5, // 非中文
Range = 6, // 范围,例如3-7,2-5,7,8
其中Range,比如"1-9,2,3-7"等,用“,”、“-”分隔即可。
以上只要不符合规则,就不能进行输入,限制较为严格,并自动过滤重复、排序等。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
public enum Input_Type
{
All = 0, // 任意
Integer = 1, // 整数
Decimal = 2, // 小数
Zh = 3, // 中文
En = 4, // 英文
NoZh = 5, // 非中文
Range = 6, // 范围
}
public class InputHelper
{
public Input_Type In_Type = Input_Type.All;
public TextBox Input_Control;
/// <summary>
/// 输入法开关
/// </summary>
/// <param name="obj">TextBox对象</param>
/// <param name="enable">开关</param>
public void Set_ImeEnable(TextBox obj, bool enable)
{
InputMethod.SetIsInputMethodEnabled(obj, enable);
}
public void Input_KeyDown(object sender, KeyEventArgs e)
{
TextBox in_put = (TextBox)sender;
bool handel = true;
if (In_Type == Input_Type.Integer)
{
if (!string.IsNullOrEmpty(in_put.Text) && in_put.Text[0] == '0')
{
goto Jmp;
}
if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
{
handel = false;
}
}
else
if (In_Type == Input_Type.Decimal)
{
if (!string.IsNullOrEmpty(in_put.Text) && in_put.Text[0] == '0')
{
if (in_put.Text.Length == 1 && e.Key != Key.Decimal && e.Key != Key.OemPeriod)
{
goto Jmp;
}
}
if (in_put.Text.Contains(".") && (e.Key == Key.Decimal || e.Key == Key.OemPeriod))
{
goto Jmp;
}
if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key == Key.Decimal || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.OemPeriod)
{
handel = false;
}
}
else
if (In_Type == Input_Type.Zh)
{
Regex reg = new Regex("^[\u4e00-\u9fa5]$");
if (reg.IsMatch(e.Key.ToString()))
{
handel = false;
}
}
else
if (In_Type == Input_Type.En)
{
if (e.Key >= Key.A && e.Key <= Key.Z)
{
handel = false;
}
}
else
if (In_Type == Input_Type.Range)
{
if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key == Key.Subtract || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || e.Key == Key.OemMinus || e.Key == Key.OemComma)
{
if (string.IsNullOrEmpty(in_put.Text) && (e.Key == Key.Subtract || e.Key == Key.OemMinus || e.Key == Key.OemComma))
{
goto Jmp;
}
if (!string.IsNullOrEmpty(in_put.Text))
{
if ((in_put.Text[in_put.Text.Length - 1] == '-' || in_put.Text[in_put.Text.Length - 1] == ',') && (e.Key == Key.Subtract || e.Key == Key.OemMinus || e.Key == Key.OemComma))
{
goto Jmp;
}
if (e.Key == Key.Subtract || e.Key == Key.OemMinus)
{
for (int i = in_put.Text.Length - 1; i >= 0; i--)
{
if (in_put.Text[i] == ',') break;
if (in_put.Text[i] == '-') goto Jmp;
}
}
}
handel = false;
}
}
Jmp: e.Handled = handel;
}
public List<int> Get_Range(TextBox obj)
{
List<int> get_range = new List<int>();
string[] splitString = obj.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < splitString.Length; i++)
{
int n;
if (int.TryParse(splitString[i], out n))
{
get_range.Add(int.Parse(splitString[i]));
}
else
{
string[] splitSpear = splitString[i].Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
if (splitSpear.Length == 2)
{
int n1;
int.TryParse(splitSpear[0], out n1);
int n2;
int.TryParse(splitSpear[1], out n2);
if (n1 > n2)
{
n1 = n1 ^ n2;
n2 = n1 ^ n2;
n1 = n1 ^ n2;
}
if ((n1 != -1) && (n2 != -1))
{
for (int t = n1; t <= n2; t++)
{
get_range.Add(t);
}
}
}
}
}
if (get_range.Count > 0) get_range = get_range.Distinct().ToList().OrderBy(x => x).ToList();
return get_range;
}
}
使用:比如输入范围,点击按钮显示出来
namespace Test_9
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
InputHelper inputHelper;
public MainWindow()
{
InitializeComponent();
inputHelper = new InputHelper();
inputHelper.In_Type = Input_Type.Range;
TextBox1.KeyDown += inputHelper.Input_KeyDown;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
List<int> num = new List<int>();
num = inputHelper.Get_Range(TextBox1);
string tmp = "";
for (int i = 0; i < num.Count; i++) tmp += num[i] + "--";
MessageBox.Show(tmp);
}
}
}
其他功能代码内查看