TextBox control increases Maximum Length property

Source: Internet
Author: User

Http://www.cnblogs.com/lemony/archive/2007/04/10/707474.html

It has always been known that in SQL Server, the varchar type is calculated in bytes. For example, varchar (50) can store 50 English characters, but can store only 25 Chinese characters (2 bytes in a Chinese character). Change to nvarchar can solve this problem, but when writing SQL statements must add n identification, otherwise it will produce garbled.

But the recent use of C # to do a database project, only to know the problem is very serious. The legality of the input should be limited in the interface, for example, for varchar (50) field, it is best to control the user's inability to enter more than 50 single-byte characters in the interface. It was then found that the MaxLength of the textbox computed only the Unicode length.

For Unicode, I'm not going to do a description here. Setting the MaxLength is not a good guarantee of the legitimacy of the input. So I decided to add a property that restricts the maximum number of bytes for the textbox: Maxbytelength.

Create a new component

Let's create a new component Textboxex, inherit from the TextBox, add a Maxbytelength property
public partial class Textboxex:textbox
{
Public Textboxex ()
{
InitializeComponent ();
}

Property #region Property

private int m_maxbytelength = 0;
[Description (] Gets or sets the maximum number of bytes that a user can type or paste in a text box control. 0 for the permissible infinite length. ")]
/**////<summary>
Gets or sets the maximum number of bytes that the user can type or paste in a text box control. 0 for the permissible infinite length.
</summary>
public int Maxbytelength
{
get {return m_maxbytelength;}
set {m_maxbytelength = value;}
}
}


Then rewrite the WndProc to determine the byte length when entering and pasting. (Fixed input "." No question of judgment)
protected override void WndProc (ref message M)
{
If this property is not set, input is allowed
if (m_maxbytelength = 0)
{
Base. WndProc (ref m);
return;
}

Switch (m.msg)
{
Case WM_CHAR:
int i = (int) M.wparam;
BOOL Isback = (i = = (int) keys.back);
BOOL Isctr = (i = = 24) | |  (i = = 22) | |  (i = = 26) | | (i = = 3);

if (Isback | | ISCTR)
{
Control keys are not processed
}
Else
{
char C = (char) i;
if (Checkbytelengthflow (c.tostring ()))
{
break;
}
}
Base. WndProc (ref m);
break;
Case Wm_paste:
IDataObject idata = Clipboard.getdataobject (); Fetching Clipboard objects
if (Idata.getdatapresent (Dataformats.text))//Judge whether Text
{
String text = (string) idata.getdata (Dataformats.text); Fetch data
if (Checkbytelengthflow (text))
{
M.result = (IntPtr) 0; Can not paste
break;
}
}
Base. WndProc (ref m);
break;
Default:
Base. WndProc (ref m);
break;
}
}

<summary>
Determine if the length of text you are about to enter overflows
</summary>
<param name= "text" > Text </param>
<returns> whether overflow </returns>
private bool Checkbytelengthflow (string text)
{
int len = getbytelength (text); The length of the character entered
int tlen = Getbytelength (this.   Text); The length of the text box's original text
int slen = Getbytelength (this.     SelectedText); text box The length of the selected text
Return (M_maxbytelength < (Tlen-slen + len));
}

<summary>
Calculate text byte length, distinguish multibyte characters
</summary>
<param name= "text" > Text </param>
<returns> text byte length </returns>
private int Getbytelength (string text)
&

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.