WPF textbox limits input by byte length

Source: Internet
Author: User

The first two days to do a project, because the page does not limit the input length of the textbox, so the background directly error, exceeding the maximum length of the database.

The length of the database is calculated in bytes, and different encoding formats, Chinese characters occupy the same length of bytes, for example, we use UTF8, a Chinese character is 3 bytes, and the default, a Chinese character is 2 bytes.

TextBox has a MaxLength property, but this property is not very desirable, because this length is limited to the length of the input, such as setting 20, whether the number, letter, the maximum length of Chinese characters are 20, but for the database, the length is not the same,

Therefore, you cannot use this property.

In order to solve this problem uniformly, the textbox is written with additional attributes.

First, the desired effect

Using the attached property, want to achieve what effect, is like setting maxlength, once to the database byte length, you can no longer input.

Therefore, the first to find a restricted input properties, unfortunately I learned too shallow, did not find the relevant properties, so the last time in a colleague's reminder, you can record the previous content, and then, if the long, the last content to be assigned

Second, additional properties

Given the additional properties and ease of use, it will definitely expose developers to at least two: Maxbytelength is used to set the maximum number of bytes, Encodemodel is used to set the encoding format

Encodemodel is made with the menu type and is easy to use when you tap content directly

Originally above is directly want to use encoding to do, but it is abstract class, had to write a method to do a conversion, and the encoding type of attributes to private.

In general, it is such a way of thinking, the following code, to the needs of people to use.

 Public classMaxbyteattachedproperty:dependencyobject {Static stringPretext ="";  Public enumEncode {Default, ASCII, UTF8, UTF32, UTF7, Bigendianunicode, Unicode} Public Static intgetmaxbytelength (DependencyObject obj) {return(int) obj.        GetValue (Maxbytelengthproperty); }         Public Static voidSetmaxbytelength (DependencyObject obj,intvalue) {obj.        SetValue (maxbytelengthproperty, value); }        //Using a DependencyProperty as the backing store for Maxbytelength. This enables animation, styling, binding, etc ...         Public Static ReadOnlyDependencyProperty Maxbytelengthproperty =dependencyproperty.registerattached ("Maxbytelength",typeof(int),typeof(Maxbyteattachedproperty),NewPropertyMetadata (ontextboxpropertychanged)); Private Static voidontextboxpropertychanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {TextBox TB = d asTextBox; if(tb==NULL)            {                return; } TB. TextChanged+=tb_textchanged; }        Private Static voidTb_textchanged (Objectsender, Textchangedeventargs e) {TextBox TB= Sender asTextBox; if(Isoutmaxbytelength (TB. TEXT,TB)) {TB. Text=pretext; Tb. Select (TB. Text.length,0); return; }        }         Public StaticEncode Getencodemodel (DependencyObject obj) {return(Encode) obj.        GetValue (Encodemodelproperty); }         Public Static voidSetencodemodel (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 ReadOnlyDependencyProperty Encodemodelproperty =dependencyproperty.registerattached ("Encodemodel",typeof(Encode),typeof(Maxbyteattachedproperty),NewPropertyMetadata (Encode.utf8, onencodemodelchanged)); Private Static voidonencodemodelchanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {Setem (D,getencodem        Odel (d)); }        Private StaticEncoding Getencodingmodel (DependencyObject obj) {return(Encoding) obj.        GetValue (Encodingmodelproperty); }        Private Static voidSetencodingmodel (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 ReadOnlyDependencyProperty Encodingmodelproperty =dependencyproperty.registerattached ("Encodingmodel",typeof(Encoding),typeof(Maxbyteattachedproperty),NewPropertyMetadata (Encoding.UTF8)); Private Static voidSetem (DependencyObject obj,encode e) {Switch(e) { CaseEncode.Default:SetEncodingModel (obj, encoding.default);  Break;  CaseEncode.ASCII:SetEncodingModel (obj, encoding.ascii);  Break;  CaseEncode.UTF8:SetEncodingModel (obj, Encoding.UTF8);  Break;  CaseEncode.UTF32:SetEncodingModel (obj, encoding.utf32);  Break;  CaseEncode.UTF7:SetEncodingModel (obj, encoding.utf7);  Break;  CaseEncode.BigEndianUnicode:SetEncodingModel (obj, encoding.bigendianunicode);  Break;  CaseEncode.Unicode:SetEncodingModel (obj, encoding.unicode);  Break; default:                     Break; }        }        Private Static BOOLIsoutmaxbytelength (stringtxt, DependencyObject obj) {            intTxtlength = Getencodingmodel (obj). GetBytes (TXT). Length;//Text Length            if(Getmaxbytelength (obj) >=txtlength) {Pretext=txt; return false; }            return true; }    }

Here's how to use it:

Maxbytelength is a must set not to default, Encodemodel can not be set but because it is our own use, so the default is UTF8, you can modify the code, in accordance with your company's coding format, so you do not have to assign value.

WPF textbox limits input by byte length

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.