Background code:
First declare a method to verify that the information in the input box meets the requirements
/// <Summary>
/// Format the string length to distinguish Chinese characters from letters. Two Chinese characters, one letter or Digit
/// </Summary>
/// <Param name = "str"> string to be formatted </param>
/// <Param name = "N"> returns the New String Length. </param>
/// <Returns> New String </returns>
Public String stringformat (string STR, int N)
{
String temp = string. empty;
Int T = 0;
Char [] q = Str. tochararray ();
If (system. Text. encoding. Default. getbytecount (STR) <= N) // If the length is less than the required length, the original string is returned.
{
For (INT I = 0; I <q. Length & T <n; I ++)
{
If (INT) Q [I]> = 0x4e00 & (INT) Q [I] <= 0x9fa5) // whether the Chinese character is used
{
Temp + = Q [I];
T + = 2;
}
Else
{
Temp + = q [I];
T + = 1;
}
}
If (t <= n)
{
Return temp;
}
Else
Return NULL;
}
Else
{
Return null;
}
}
In the input box, add:
Private void txbName_LostFocus (object sender, RoutedEventArgs e)
{
If (txbName. Text. Length> 0)
{
CheckTextBox chkTxb = new CheckTextBox ();
String str = chkTxb. stringFormat (txbName. Text, 20 );
If (str = null)
MessageBox. Show ("the length of the entered characters is incorrect! ");
Else
TxbName. Text = str;
}
}