Adds a string of 2 characters to a space and formats it in uppercase

Source: Internet
Author: User

<summary>
Format the input text box (allow only letters to be entered and space slots to be added every two characters)
</summary>
public class Formattextbox:radtextbox
{
#region Define, attribute

<summary>
is the BACKSPACE key
</summary>
private bool Isemptykey = false;

// <summary>
/// enum type, false means no conversion, true for uppercase conversion
// </summary>
Public enum Touppers
{
False,
True,
}
private touppers To_upper;
[DefaultValue (Touppers.false)]
Public touppers To_upper
{
Get
{
return to_upper;
}
Set
{
to_upper = value;
}
}

#endregion

#region Verride Event

protected override void OnKeyPress (System.Windows.Forms.KeyPressEventArgs e)
{
Base. OnKeyPress (e);

Setstandard (e);
}

// <summary>
///Only allow entry of letters and BACKSPACE
// </summary>
/// <param name= "E" > Keyboard keys </param>
private void Setstandard (System.Windows.Forms.KeyPressEventArgs e)
{
//Allow only letters to be entered
regex regex = new Regex ("^[a-za-z\b]");
MatchCollection mc = regex. Matches (E.keychar.tostring ());
foreach (Match MA in MC)
{
e.handled = false;
if (E.keychar = = ' \b ')
{
Isemptykey = true;
}
return;
}

E.handled = true;
}

protected override void OnTextChanged (EventArgs e)
{
Base. OnTextChanged (e);

if (ToUppers.True.ToString () = = To_upper.tostring ())
{
This. Text = this. Text.toupper ();
This. Select (text.length, 0);
}
}

Public override string Text
{
Get
{
return base. Text;
}
Set
{
//BACKSPACE bar
if (isemptykey)
{
base. Text = value;
Isemptykey = false;
return;
}
if (value. Length < 2)
{
base. Text = value;
return;
}

StringBuilder sb = new StringBuilder ();
string content = value. Trim ();
content = content. Replace ("", String. Empty);
Try
{
int length = content. LENGTH/2;
int num = 2;
if (content. Length > 0)
{
for (int i = 0; i < length; i++)
{
num = 2 * i;
string tempstr = content. Substring (num, 2);
sb. Append (TEMPSTR);
sb. Append ("");
}
}
Append last character when//length is odd
int remainder = content. Length% 2;
if (remainder = = 1)
{
string tempstr = content. Substring (content. Length-1, 1);
sb. Append (TEMPSTR);
}
}
Catch
{
return;
}

Base. Text = sb. ToString (). ToUpper ();
Return
}
}

#endregion

}

Adds a string of 2 characters to a space and formats it in uppercase

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.