Enter the WPF TextBox by byte length, wpftextbox

Source: Internet
Author: User

Enter the WPF TextBox by byte length, wpftextbox

When I was working on a project two days ago, because the input length of TextBox was not limited on the page, an error was reported directly in the background, exceeding the maximum length of the database.

The database length is calculated by byte, and the length of Chinese Characters in different encoding formats is different. For example, we use UTF8 and a Chinese character is three bytes, by Default, one Chinese character is 2 bytes.

TextBox has a MaxLength attribute, but this attribute is not suitable, because this length limits the input length, such as setting 20, the maximum length of numbers, letters, and Chinese characters is 20, but the length of the database is different,

Therefore, this attribute cannot be used.

To solve this problem in a unified manner, an additional attribute is written to TextBox.

 

I. desired results

With the additional attribute, what effect does it achieve? Just like setting MaxLength, once the database reaches the byte length, it will no longer be input.

Therefore, I first wanted to find an attribute that restricts input. Unfortunately, I learned it too lightly and did not find the relevant attribute. Therefore, I can record the previous content with my colleague's reminder, then, if it is too long, assign a value using the previous content.

Ii. Additional attributes

Since additional attributes are used and convenient to use, it must be exposed to developers at least two: MaxByteLength is used to set the maximum number of bytes, and EncodeModel is used to set the encoding format.

EncodeModel is made of the Menu type, so that you can directly tap the content during use.

Originally, we directly wanted to use Encoding as an abstract class. Instead, we had to write a method for conversion and private the Encoding type attribute.

 

In general, this is the way of thinking. The code below is used by the people who need it.

Public class MaxByteAttachedProperty: DependencyObject {static string preText = ""; public enum Encode {Default, ASCII, UTF8, UTF32, UTF7, BigEndianUnicode, Unicode} 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, subject 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 = preText; 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, effece) {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; // text Length if (GetMaxByteLength (obj)> = txtLength) {preText = txt; return false;} return true ;}}

The usage is as follows:

MaxByteLength is mandatory and is not set by default. EncodeModel can not be set but is used by ourselves, so the default value is UTF8. You can modify the code by yourself according to your company's encoding format, in this way, no value is required.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.