自定义针对Product Key处理的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 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的更多相关文章
- Microsoft Office 2013 Product Key
Microsoft Office 2013 Product Key ( Professional Plus ) PGD67-JN23K-JGVWW-KTHP4-GXR9G B9GN2-DXXQC-9D ...
- Get your Windows product key from a script
The product key is located in the registry under HKLM\Software\Microsoft\Windows NT\CurrentVersion I ...
- map以自定义类型当Key
关于map的定义: template < class Key, class T, class Compare = less<Key>, class Allocator = alloc ...
- 如何更换Office 2013的product key?
第一步 第二步 第三步 ... ... ... ... ^_^ 参考资料 ======================== Change Product Key Office 2013 Home ...
- 【转载】【zabbix】自定义监控项key值
[转载]https://www.cnblogs.com/zhenglisai/p/6547402.html [zabbix]自定义监控项key值 说明: zabbix自带的默认模版里包括了很多监控 ...
- 关于MapReduce中自定义带比较key类、比较器类(二)——初学者从源码查看其原理
Job类 /** * Define the comparator that controls * how the keys are sorted before they * are pa ...
- Python——dict(自定义类作key)
Python的dict要求key为不可变数据类型,通常采用str或int,但在某些应用场景下,需要采用自定义类型对象作key, 此时的自定义类需要实现两个特殊方法:__hash__.__eq__,用于 ...
- 【zabbix】自定义监控项key值
说明: zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,这里介绍一种自定义监控项的方式. 1,首先编写自定义监控脚本,本文以监控httpd进程 ...
- Prometheus 自定义exporter 监控key
当Prometheus的node_exporter中没有我们需要的一些监控项时,就可以如zabbix一样定制一些key,让其支持我们所需要的监控项. 例如,我要根据 逻辑cpu核数 来确定load的告 ...
随机推荐
- Python学习-21.Python的代码注释
在Python中有两种注释,一种是普通注释,另一种是文档注释. 普通注释是使用#开头 print('output something') # here is comment 而Python中多行注释也 ...
- Python学习-18.Python中的错误处理(三)
在某些情况下,我们需要定义自己的异常并且抛出 先定义一个错误: class MyError(BaseException): def __init__(self): pass 上面定义了一个叫MyErr ...
- android应用搬家的实现
android手机上的应用搬家功能,具体的介绍和原理参考: 系统目录及应用搬家的研究 应用搬家的实现 这里主要是作为一个补充,因为上面两篇文章虽然讲的挺好的,但是给出的例子不能直接运行,还是需要一些准 ...
- DBCC--EXTENTINFO/IND/PAGE--显示数据页信息
DBCC EXTENTINFO得到对象分配的区DBCC EXTENTINFO(<dbname|dbid>,<tabelname|tableid>[,{indexname|ind ...
- 构建NetCore应用框架之实战篇(五):BitAdminCore框架1.0登录功能设计实现及源码
本篇承接上篇内容,如果你不小心点击进来,建议从第一篇开始完整阅读,文章内容继承性连贯性. 构建NetCore应用框架之实战篇系列 一.设计原则 1.继承前面框架架构思维,设计以可读性作为首要目标. 2 ...
- Netbeans8.1设置Consola字体并解决中文乱码问题
netbeans是php非常好用的ide,并且还是免费的!但是好多字体不支持中文,会显示如下乱码: 解决方法如下: 通过修改jre的配置文件成功解决了这个问题. 1. 进入jdk安装目录下/jre/l ...
- 枚举类型内部函数 enumerate
enumerate()说明enumerate()是python的内置函数enumerate在字典上是枚举.列举的意思对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enumera ...
- 935. Knight Dialer
A chess knight can move as indicated in the chess diagram below: . This time, we place o ...
- 887. Super Egg Drop
You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is ident ...
- python 使用dir() help() 查看一个对象所有拥有的方法和属性
可以使用python 的内置方法 dir() 或者help() 查看 某个对象所拥有的方法和属性, 二者间的区别是: dir() : 只是得到方法或者属性的名称 help():不但可以得到对象的方法和 ...