代码:

 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. Quartus II中使用脚本转换sof到rbf文件

    1.  新建一个文本文件,保存为任意但有意义的名字,如:sof_to_rbf.bat,注意,保存时请不要使用默认的格式,应该手动从.txt切换为all files 2.  在文本中输入以下内容: %Q ...

  2. poj1836 Alignment

    Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11707   Accepted: 3730 Descri ...

  3. 微软在线实验室启用谷歌的reCAPTCHA,我们又丢失了一个好东东

    在没有启用reCAPTCHA的日子,我们可以在微软的在线实验室www.microsoft.com/handsonlabs 中找到许许多多的文档.视频.动手实验环境. 不需要任何硬件.技术,就可以快速的 ...

  4. oracle 11g Enterprise Manager配置失败

    Enterprise Manager以下简称em,Database Configuration Assistant简称DBCA. 病症 监听程序未启动或数据库服务未注册到该监听程序.启动该监听程序并注 ...

  5. java spring boot 开启监控信息

    效果: 配置 // pom <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  6. Layui:前后端分离之Form表单

    页面效果图: <div style="display: none;" id="formContainer"> <form class=&quo ...

  7. C#构造方法(函数)

    一.概括 1.通常创建一个对象的方法如图: 通过  Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初始化对象的,为类的成员赋值 ...

  8. vs 生成事件 +版本号+sed.exe

    set ASMINFO=Properties\AssemblyInfo.csFINDSTR /C:"[assembly: AssemblyVersion(" %ASMINFO% | ...

  9. .NET Core 玩一玩 Ocelot API网关

    .net 这几年国内确实不好过. 很多都选择转行.不过.net Core跨平台 开源之后 .社区的生态在慢慢建立.往好的趋势发展. 对于坚守在.NET战线的开发者来说 是个挺不错的消息.  特别是微软 ...

  10. The Beam Model:Stream & Tables翻译(上)

    作者:周思华 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文尝试描述Beam模型和Stream & Table理论间的关系(前者描述于数据流模型论文.the-world-beyond ...