<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