- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章WPF TextBox实现按字节长度限制输入功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前两天做一个项目的时候,由于页面没有限制textbox的输入长度,所以,后台直接报错了,超出数据库最大的长度.
数据库的长度是按照字节来计算的,而且不同的编码格式,汉字占用的字节长度又不相同,比如,我们用的是utf8,一个汉字是3个字节,而默认的default,一个汉字是2个字节.
textbox有个maxlength属性,但是这个属性是不太合乎要求的,因为这个长度,是限制了输入的长度,比如设置20,则无论是数字、字母、汉字最大的长度都是20个,但是,对于数据库来说,长度却不相同了,所以,不能使用这个属性.
为了,统一解决下这个问题,所以给textbox写了附加属性.
1、想要的效果 。
用了附加属性,想达到一个什么效果呢,就是像设置maxlength一样,一旦到了数据库的字节长度,就不再能输入了.
因此,最开始想找一个限制输入的属性,可惜我学的太浅薄,没有找到相关的属性,因此,最后在同事的提醒下,可以记录上一次的内容,然后,如果超长,就用上一次的内容进行赋值 。
2、附加属性 。
既然要用附加属性,并且方便使用,那肯定要给开发者暴露出来至少两个:maxbytelength用来设置最大的字节数,encodemodel用来设置编码格式 。
encodemodel是用menu类型来做的,方便使用时直接敲内容 。
本来上面是直接想用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
;
}
}
|
使用方法如下:
maxbytelength是必须设置的没有进行默认,encodemodel可以不设置但是由于是我们自己用,所以默认是utf8,可以自行修改代码,按照你们公司的编码格式,这样也就不用赋值了.
代码已修正,感谢presia发现的bug,疏忽了.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.cnblogs.com/ZXdeveloper/archive/2017/11/07/7798943.html 。
最后此篇关于WPF TextBox实现按字节长度限制输入功能的文章就讲到这里了,如果你想了解更多关于WPF TextBox实现按字节长度限制输入功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
将 KLV 字符串拆分为键、长度、值作为元素的列表/元组的更有效方法是什么? 为了添加一点背景,前 3 位数字作为键,接下来的 2 位表示值的长度。 我已经能够使用以下代码解决该问题。但我不认为我的代
首先,我试图从文件中提取视频持续时间,然后在无需实际上传文件的情况下显示它。 当用户选择视频时 - 信息将显示在其下方,包括文件名、文件大小、文件类型。不管我的技能多么糟糕 - 我无法显示持续时间。我
我是 Scala 编程新手,这是我的问题:如何计算每行的字符串数量?我的数据框由一列 Array[String] 类型组成。 friendsDF: org.apache.spark.sql.DataF
我有一个React Web应用程序(create-react-app),该应用程序使用react-hook-forms上传歌曲并使用axios将其发送到我的Node / express服务器。 我想确
如果给你一个网络掩码(例如 255.255.255.0),你如何在 Java 中获得它的长度/位(例如 8)? 最佳答案 如果您想找出整数低端有多少个零位,请尝试 Integer.numberOfTr
我需要使用 jQuery 获取 div 数量的长度。 我可以得到它,但在两个单击事件中声明变量,但这似乎是错误的,然后我还需要使用它来根据数字显示隐藏按钮。我觉得我不必将代码加倍。 在这里摆弄 htt
我对此感到非常绝望,到目前为止我在 www 上找不到任何东西。 情况如下: 我正在使用 Python。 我有 3 个数组:x 坐标、y 坐标和半径。 我想使用给定的 x 和 y 坐标创建散点图。 到目
我有一个表单,我通过 jQuery 的加载函数动态添加新的输入和选择元素。有时加载的元素故意为空,在这种情况下我想隐藏容器 div,这样它就不会破坏样式。 问题是,我似乎无法计算加载的元素,因此不知道
我决定通过替换来使我的代码更清晰 if (wrappedSet.length > 0) 类似 if (wrappedSet.exists()) 是否有任何 native jq 函数可以实现此目的?或者
简单的问题。如果我有一个如下表: CREATE TABLE `exampletable` ( `id` int(11) NOT NULL AUTO_INCREMENT, `textfield`
我正在使用经典 ASP/MySQL 将长用户输入插入到我的数据库中,该输入是从富文本编辑器生成的。该列设置为 LONG-TEXT。 作为参数化查询(准备语句)的新手,我不确定用于此特定查询的数据长度。
我正在获取 Stripe 交易费用的值(value)并通过禁用的文本字段显示它。 由于输入文本域,句子出现较大空隙 This is the amount $3.50____________that n
我有一个 div,其背景图像的大小设置为包含。但是,图像是视网膜计算机(Macbook Pro 等)的双分辨率图像,所以我希望能够以某种方式让页面知道即使我说的是背景大小:包含 200x200 图像,
我正在开发一个具有“已保存”和“已完成”模块的小部件。当我删除元素时,它会从 dom 中删除/淡化它,但是当我将其标记为完成时,它会将其克隆到已完成的选项卡。这工作很棒,但顶部括号内的数字不适合我。这
我有一个来自 json 提要的数组,我知道在 jArray 中有一个联盟,但我需要计算出该数组的计数,以防稍后将第二个添加到提要中。目前 log cat 没有注销“teamFeedStructure”
目标:给定一个混合类型的数组,确定每个级别的元素数量。如果同一层有两个子数组,则它们的每个元素都计入该层元素的总数。 方法: Array.prototype.elementsAtLevels = fu
我需要帮助为 Java 中的单链表制作 int size(); 方法。 这是我目前所拥有的,但它没有返回正确的列表大小。 public int size() { int size = 0;
我正在为学校作业创建一个文件服务器应用程序。我目前拥有的是一个简单的 Client 类,它通过 TCP 发送图像,还有一个 Server 类接收图像并将其写入文件。 这是我的客户端代码 import
我有这对功能 (,) length :: Foldable t => t a -> b -> (Int, b) 和, head :: [a] -> a 我想了解的类型 (,) length he
我正在GitHub Pages上使用Jekyll来构建博客,并希望获得传递给YAML前题中Liquid模板的page.title字符串的长度,该字符串在每个帖子的YAML主题中。我还没有找到一种简单的
我是一名优秀的程序员,十分优秀!