代码:

 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 DriverEasyWPF.Utils
{
public class LicenseTextBox : TextBox
{
private bool isDelete = false;
public int BasicLength { get; set; }//每段长度
public int SumCount { get; set; }//总共几段
public String SpChar { get; set; }//分隔的符号
public bool ShowSpCharFirst { get; set; }
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e); if (ShowSpCharFirst)
Do();
else
Do2(); } private void Do()
{
//SetCaretIndex();
CheckWhetherDelete(); InitData();
/*
* eg: 12345-12345-12345-12345-12345
*/
if (!isDelete)
{
for (int i = ; i < SumCount - ; i++)
{
int length = BasicLength * (i + );
AppendSubtractByLength(length + i);
IgnoreInputSubtract(length + (i + ), SpChar);
}
int maxLength = BasicLength * SumCount + (SumCount - );
if (Text.Length >= maxLength || (Text.Length >= SumCount*BasicLength && Text.IndexOf(SpChar)<=))
{
//Text = Text.Substring(0, maxLength);//旧代码
//Regex reg = new Regex("[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}");
//原型是:^([A-Z0-9]{{0}}-){4}[A-Z0-9]{{5}}$
String regPattern = "^([A-Z0-9]{"+BasicLength+"}-){"+(SumCount-)+"}[A-Z0-9]{"+BasicLength+"}$";
Regex reg = new Regex(regPattern);
if (!reg.IsMatch(Text))
{
String content = Text.Replace(SpChar, String.Empty);
Text = content.Substring(, BasicLength) + SpChar
+ content.Substring(BasicLength, BasicLength) + SpChar
+ content.Substring(BasicLength * , BasicLength) + SpChar
+ content.Substring(BasicLength * , BasicLength) + SpChar
+ content.Substring(BasicLength * , BasicLength);
}
SetCaretIndex();
}
}
} private void Do2()
{
//SetCaretIndex();
CheckWhetherDelete(); InitData();
/*
* eg: 12345-12345-12345-12345-12345
*/
if (!isDelete)
{
for (int i = ; i < SumCount - ; i++)
{
int length = BasicLength * (i + );
EqualsWithoutSpChar(length + i);
EqualsWithSpChar(length + i);
} #region old logic
/*
if (Text.Length == 6 && Text[5].ToString().Equals(SpChar))
Text = Text;
if (Text.Length == 6 && !Text[5].ToString().Equals(SpChar))
{
Text = Text.Substring(0, 5) + SpChar + Text.Substring(5);
SetCaretIndex();
} if (Text.Length == 12 && Text[11].ToString().Equals(SpChar))
Text = Text;
if (Text.Length == 12 && !Text[11].ToString().Equals(SpChar))
{
Text = Text.Substring(0, 11) + SpChar + Text.Substring(11);
SetCaretIndex();
} if (Text.Length == 18 && Text[17].ToString().Equals(SpChar))
Text = Text;
if (Text.Length == 18 && !Text[17].ToString().Equals(SpChar))
{
Text = Text.Substring(0, 17) + SpChar + Text.Substring(17);
SetCaretIndex();
} if (Text.Length == 24 && Text[23].ToString().Equals(SpChar))
Text = Text;
if (Text.Length == 24 && !Text[23].ToString().Equals(SpChar))
{
Text = Text.Substring(0, 23) + SpChar + Text.Substring(23);
SetCaretIndex();
}
*/
#endregion int maxLength = BasicLength * SumCount + (SumCount - );
if (Text.Length > maxLength)
{
Text = Text.Substring(, maxLength);
SetCaretIndex();
}
}
} private void EqualsWithSpChar(int length)
{
if (Text.Length == (length+) && !Text[length].ToString().Equals(SpChar))
{
Text = Text.Substring(, length) + SpChar + Text.Substring(length);
SetCaretIndex();
}
} private void EqualsWithoutSpChar(int length)
{
if (Text.Length == (length+) && Text[length].ToString().Equals(SpChar))
Text = Text;
} /// <summary>
/// 默认是 5段,每段5个长度
/// </summary>
private void InitData()
{
if (BasicLength <= )
BasicLength = ; if (SumCount <= )
SumCount = ; if (String.IsNullOrEmpty(SpChar))
SpChar = "-"; } private void CheckWhetherDelete()
{
if (Keyboard.IsKeyDown(Key.Delete) || Keyboard.IsKeyDown(Key.Back))
isDelete = true;
else
isDelete = false;
} private void SetCaretIndex()
{
//this.CaretIndex = Text.Length;
this.Focus();
this.SelectionStart = Text.Length;
} private void IgnoreInputSubtract(int length, String subChar)
{
if (Text.Length == length && Text[length - ].ToString() == subChar)
{
Text = Text.Substring(, length - );
SetCaretIndex();
}
} private void AppendSubtractByLength(int length)
{
if (Text.Length == length)
{
Text += SpChar;
SetCaretIndex();
}
} }
}

使用:

  <!--现在xaml的文件头,添加相应的命名空间-->
xmlns:model="clr-namespace:LicenseKeyTextBoxDemo.Model" <!--用法如下-->
<model:LicenseTextBox Margin="88,220,70,255" BasicLength="5" SumCount="5" SpChar="-" IsMSMode="true"/>

想法:

改进:

自定义针对Product Key处理的TextBox的更多相关文章

  1. Microsoft Office 2013 Product Key

    Microsoft Office 2013 Product Key ( Professional Plus ) PGD67-JN23K-JGVWW-KTHP4-GXR9G B9GN2-DXXQC-9D ...

  2. Get your Windows product key from a script

    The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...

  3. map以自定义类型当Key

    关于map的定义: template < class Key, class T, class Compare = less<Key>, class Allocator = alloc ...

  4. 如何更换Office 2013的product key?

    第一步 第二步 第三步 ... ... ... ... ^_^   参考资料 ======================== Change Product Key Office 2013 Home ...

  5. 【转载】【zabbix】自定义监控项key值

    [转载]https://www.cnblogs.com/zhenglisai/p/6547402.html [zabbix]自定义监控项key值   说明: zabbix自带的默认模版里包括了很多监控 ...

  6. 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理

    Job类 /**   * Define the comparator that controls    * how the keys are sorted before they   * are pa ...

  7. Python——dict(自定义类作key)

    Python的dict要求key为不可变数据类型,通常采用str或int,但在某些应用场景下,需要采用自定义类型对象作key, 此时的自定义类需要实现两个特殊方法:__hash__.__eq__,用于 ...

  8. 【zabbix】自定义监控项key值

    说明: zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,这里介绍一种自定义监控项的方式. 1,首先编写自定义监控脚本,本文以监控httpd进程 ...

  9. Prometheus 自定义exporter 监控key

    当Prometheus的node_exporter中没有我们需要的一些监控项时,就可以如zabbix一样定制一些key,让其支持我们所需要的监控项. 例如,我要根据 逻辑cpu核数 来确定load的告 ...

随机推荐

  1. Spring框架事务支持模型的优势

    全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...

  2. Android-ColorsUtil工具类

    颜色工具类 public class ColorsUtil { private ColorsUtil() { throw new Error("Do not need instantiate ...

  3. Python学习-17.Python中的错误处理(二)

    错误是多种多样的,在 except 语句中,可以捕获指定的异常 修改代码如下: import io path = r'' mode = 'w' try: file = open(path,mode) ...

  4. [Openwrt 项目开发笔记]:DDNS设置(五)

    [Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一节中,我主要讲述了如何在Openwrt上安 ...

  5. Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路

    Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路 那就是利用mvvmlight中的messager组件(可单独提取出),制作双向的一个消息发送.通过公共的类的定义,来传递数 ...

  6. 接口interface和抽象类型abstract

    一.接口 接口不能被实例化 接口只能包含方法声明 接口的成员包括方法.属性.索引器.事件 接口中不能包含常量.字段(域).构造函数.析构函数.静态成员 接口中的所有成员默认为public,因此接口中不 ...

  7. Mysql-基础+安装指南

    安装指南: https://www.cnblogs.com/majj/p/9160383.html  小马哥 下载完后初始化操作数据库: 1. 将文件放在 : G:\软件\mysql-8.0.15-w ...

  8. C - 前m大的数 (结构体)

    点击打开链接 还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的 ...

  9. django模型中, 外键字段使用to_filed属性 指定到所关联主表的某个字段

    在django项目的开发过程中,在设计模型时一开始将主键设置成了一个自定义的字段,但是在创建搜索索引时却发现必须要存在一个id的字段,并且为主键(不知道是否是项目一开始就这样配置的原因), 但此时表结 ...

  10. 二,mysql优化——sql优化基本概念

    1,SQL优化的一般步骤 (1)通过show status命令了解各种SQL执行效率. (2)通过执行效率较低的SQL语句(重点select). (3)通过explain分析低效率的SQL语句的执行情 ...