代码:

 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. springMVC 开涛 数据绑定

    纸上得来终觉浅,绝知此事要躬行. 一.@requestParam //使用方法URL:?username="sfp" test(@RequestParam(value=" ...

  2. Oracle EBS中有关Form的触发器的执行顺序

    http://blog.csdn.net/postfxj/article/details/8135769 触发器执行顺序: 1.  当打开FORM时: (1)       PRE-FORM (2)   ...

  3. 开源WebGIS实施方案(六):空间数据(PostGIS)与GeoServer服务迁移

    研发环境的变更,或者研发完成进行项目现场实施.运维的时候,经常就会面临数据及服务的迁移,这其中就包含空间数据以及GeoServer服务的迁移工作. 这里需要提醒的是:如果采用的是类似的开源WebGIS ...

  4. leetcode 之突然不做了

    最近心情也不好,学不会的东西太多,以前能懂为什么,现在完全不知道为什么,只能依葫芦画瓢了,所以我写出了的代码到底是会了吗?还是瓢画的好? 热血之三分钟热度小张发现leetcode里会做的好像都做了,剩 ...

  5. OpenSL的使用

    #include <jni.h> #include <string> #include <SLES/OpenSLES.h> #include <SLES/Op ...

  6. 洛谷P4197 Peaks&&克鲁斯卡尔重构树学习笔记(克鲁斯卡尔重构树+主席树)

    传送门 据说离线做法是主席树上树+启发式合并(然而我并不会) 据说bzoj上有强制在线版本只能用克鲁斯卡尔重构树,那就好好讲一下好了 这里先感谢LadyLex大佬的博客->这里 克鲁斯卡尔重构树 ...

  7. MySQL(存储过程,支持事务操作)

    day61 保存在MySQL上的一个别名   >   一坨SQL语句 -- delimiter // -- create procedure p1() -- BEGIN -- select * ...

  8. getopt() getopt_long()函数手册[中文翻译]

    getopt()函数 getopt_long函数 函数原型(function prototype) #include <unistd.h> int getopt(int argc, cha ...

  9. Java之常用类库

    1.Java常用类库(一):http://blog.csdn.net/e6894853/article/details/7925469 1.Java常用类库(二):http://blog.csdn.n ...

  10. 语法糖----C#的async和await

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. public class MyClass { public MyClass() { DisplayValue ...