代码:

 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. MFC OnOk(),OnCancel(),OnClose(),OnDestroy()的区别总结

    MFC OnOk(),OnCancel(),OnClose(),OnDestroy()的区别总结(转) 第一,OnOK()和OnCancel()是CDialog基类的成员函数,而OnClose()和O ...

  2. hdu 4972 根据每轮篮球赛分差求结果

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 两支球队进行篮球比赛,每进一次球后更新比分牌,比分牌的计数方法是记录两队比分差的绝对值,每次进球的分可能是 ...

  3. hdu 4998 矩阵表示旋转

    http://acm.hdu.edu.cn/showproblem.php?pid=4998 http://blog.csdn.net/wcyoot/article/details/33310329 ...

  4. PHP 7 安装 Memcache 和 Memcached 总结

    Memcache 与 Memcached 的区别 Memcached 是 Memcache 的升级版,优化了 Memcache,并增加了一些操作方法.所以现在基本都是用最近版本的. PHP 7 下安装 ...

  5. [leetcode] 15. Plus One

    这道题其实让我意识到了我的英文水平还有待加强.... 题目如下: Given a non-negative number represented as an array of digits, plus ...

  6. PHP/ThinkPHP5 框架集成微博登录入库流程示意

    PHP/ThinkPHP5 框架集成微博登录入库流程示意 第三方登陆这个东东,目前主要是 微信.微博.qq.淘宝.支付宝 等几个.他们都是基于oath2协议的.原理差不多.这里记录的是我测试的新郎微博 ...

  7. react-router-dom 手动控制路由跳转

    基于 react-router 4.0 版本,我们想要通过 JS 手动控制路由跳转,分三步: 第一步:引入 propTypes const PropTypes = require('prop-type ...

  8. UITableView 的常用可复制代码

    UITableView是使用中最常用的工具,下面列举一个常用的tableview类,以后直接复制代码,稍作修改,就能用了. #import "ViewController.h" @ ...

  9. php内核为变量的值分配内存的几个宏

    在php5.3之前,为某变量分配内存是用宏 MAKE_STD_ZVAL; 737 #define MAKE_STD_ZVAL(zv) \ # /Zend/zend.h738 ALLOC_ZVAL(zv ...

  10. 【NOIP2017】列队 splay

    当年太菜了啊,连$60$分的暴力都没拿满,只打了一个$30$分的. 考虑到这题最多只会询问到$30W$个点,且整个矩阵会去到$30W\times 30W$,显然不能将所有的点存下来. 对于每一行(除最 ...