服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - WPF TextBox实现按字节长度限制输入功能

WPF TextBox实现按字节长度限制输入功能

2022-01-25 14:20衆尋 C#

这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前两天做一个项目的时候,由于页面没有限制textbox的输入长度,所以,后台直接报错了,超出数据库最大的长度。

数据库的长度是按照字节来计算的,而且不同的编码格式,汉字占用的字节长度又不相同,比如,我们用的是utf8,一个汉字是3个字节,而默认的default,一个汉字是2个字节。

textbox有个maxlength属性,但是这个属性是不太合乎要求的,因为这个长度,是限制了输入的长度,比如设置20,则无论是数字、字母、汉字最大的长度都是20个,但是,对于数据库来说,长度却不相同了,所以,不能使用这个属性。

为了,统一解决下这个问题,所以给textbox写了附加属性。

一、想要的效果

用了附加属性,想达到一个什么效果呢,就是像设置maxlength一样,一旦到了数据库的字节长度,就不再能输入了。

因此,最开始想找一个限制输入的属性,可惜我学的太浅薄,没有找到相关的属性,因此,最后在同事的提醒下,可以记录上一次的内容,然后,如果超长,就用上一次的内容进行赋值

WPF TextBox实现按字节长度限制输入功能

WPF TextBox实现按字节长度限制输入功能

二、附加属性

既然要用附加属性,并且方便使用,那肯定要给开发者暴露出来至少两个:maxbytelength用来设置最大的字节数,encodemodel用来设置编码格式

encodemodel是用menu类型来做的,方便使用时直接敲内容

WPF TextBox实现按字节长度限制输入功能

本来上面是直接想用encoding来做的,奈何它是抽象类,只好,写个方法进行了一部转化,并且把encoding类型的属性进行private。

大致上也就是这么一个思路,下面上代码,给需要的人使用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
public class maxbyteattachedproperty : dependencyobject
 {
 public enum encode
 {
  default,
  ascii,
  utf8,
  utf32,
  utf7,
  bigendianunicode,
  unicode
 }
 
 
 private static string getpretext(dependencyobject obj)
 {
  return (string)obj.getvalue(pretextproperty);
 }
 
 private static void setpretext(dependencyobject obj, string value)
 {
  obj.setvalue(pretextproperty, value);
 }
 
 // using a dependencyproperty as the backing store for pretext. this enables animation, styling, binding, etc...
 private static readonly dependencyproperty pretextproperty =
  dependencyproperty.registerattached("pretext", typeof(string), typeof(maxbyteattachedproperty), new propertymetadata(""));
 
 
 public static int getmaxbytelength(dependencyobject obj)
 {
  return (int)obj.getvalue(maxbytelengthproperty);
 }
 
 public static void setmaxbytelength(dependencyobject obj, int value)
 {
  obj.setvalue(maxbytelengthproperty, value);
 }
 
 // using a dependencyproperty as the backing store for maxbytelength. this enables animation, styling, binding, etc...
 public static readonly dependencyproperty maxbytelengthproperty =
  dependencyproperty.registerattached("maxbytelength", typeof(int), typeof(maxbyteattachedproperty), new propertymetadata(ontextboxpropertychanged));
 
 private static void ontextboxpropertychanged(dependencyobject d, dependencypropertychangedeventargs e)
 {
  textbox tb = d as textbox;
  if (tb == null)
  {
  return;
  }
  tb.textchanged += tb_textchanged;
 }
 
 private static void tb_textchanged(object sender, textchangedeventargs e)
 {
  textbox tb = sender as textbox;
  if (isoutmaxbytelength(tb.text, tb))
  {
  tb.text = getpretext(tb);
  tb.select(tb.text.length, 0);
  return;
  }
 }
 
 public static encode getencodemodel(dependencyobject obj)
 {
  return (encode)obj.getvalue(encodemodelproperty);
 }
 
 public static void setencodemodel(dependencyobject obj, encode value)
 {
  obj.setvalue(encodemodelproperty, value);
 }
 
 // using a dependencyproperty as the backing store for encodem. this enables animation, styling, binding, etc...
 public static readonly dependencyproperty encodemodelproperty =
  dependencyproperty.registerattached("encodemodel", typeof(encode), typeof(maxbyteattachedproperty), new propertymetadata(encode.utf8, onencodemodelchanged));
 private static void onencodemodelchanged(dependencyobject d, dependencypropertychangedeventargs e)
 {
  setem(d, getencodemodel(d));
 }
 
 private static encoding getencodingmodel(dependencyobject obj)
 {
  return (encoding)obj.getvalue(encodingmodelproperty);
 }
 
 private static void setencodingmodel(dependencyobject obj, encoding value)
 {
  obj.setvalue(encodingmodelproperty, value);
 }
 
 // using a dependencyproperty as the backing store for encodingmodel. this enables animation, styling, binding, etc...
 private static readonly dependencyproperty encodingmodelproperty =
  dependencyproperty.registerattached("encodingmodel", typeof(encoding), typeof(maxbyteattachedproperty), new propertymetadata(encoding.utf8));
 
 private static void setem(dependencyobject obj, encode e)
 {
  switch (e)
  {
  case encode.default:
   setencodingmodel(obj, encoding.default);
   break;
  case encode.ascii:
   setencodingmodel(obj, encoding.ascii);
   break;
  case encode.utf8:
   setencodingmodel(obj, encoding.utf8);
   break;
  case encode.utf32:
   setencodingmodel(obj, encoding.utf32);
   break;
  case encode.utf7:
   setencodingmodel(obj, encoding.utf7);
   break;
  case encode.bigendianunicode:
   setencodingmodel(obj, encoding.bigendianunicode);
   break;
  case encode.unicode:
   setencodingmodel(obj, encoding.unicode);
   break;
  default:
   break;
  }
 }
 
 private static bool isoutmaxbytelength(string txt, dependencyobject obj)
 {
  int txtlength = getencodingmodel(obj).getbytes(txt).length;//文本长度
  if (getmaxbytelength(obj) >= txtlength)
  {
  setpretext(obj, txt);
  return false;
  }
  return true;
 }
 }

使用方法如下:

WPF TextBox实现按字节长度限制输入功能

maxbytelength是必须设置的没有进行默认,encodemodel可以不设置但是由于是我们自己用,所以默认是utf8,可以自行修改代码,按照你们公司的编码格式,这样也就不用赋值了。

 代码已修正,感谢presia发现的bug,疏忽了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/ZXdeveloper/archive/2017/11/07/7798943.html

延伸 · 阅读

精彩推荐
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20