The simplest answer is to set the MaxLength property to 12.
There are ways around that simple setting with copy-paste etc. If the MaxLength is all you need then just accept the solution. If you need to deal with the other issues too, then comment below and I'll add to this solution.
Hope that helps ^_^
Andy
UPDATE: I have learnt from comments that the OP requires max of 12 numbers to be entered (thx Maciej Los ^_^):
If you want the field to be numeric only then there are ways to do this too:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
Combine this with the MaxLength and you will have a numeric field 12 chars long.
If you want a textfield for x number of chars that may contain a max on 12 digits, then you can again use the keypress event, just don't set MaxLength:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
if((sender as TextBox).Text.Count(Char.IsDigit)>=12)
e.Handled = true;
}
}
I hope this solution better suites your needs ^_^
UPDATE from OPs comments:
OP is using a RadMaskedEditBox from the Telerik suite.
The RadMaskedEditBox has it's own handlers for restricting input. These are called Masks (hence radMASKEDeditbox ^_^)
If you use these then you do not require custom format events. The RadMaskedEditBox will do it for you.
You can find the properties in the controls Properties window, or you can add them on form load.
MaskType = Standard (from dropdown); //The controls default masking
Mask = "0000000000" (enter as text); // The controls mask pattern. This represents 12 digits
If you can't find these options in the properties window then add them on form load like this:
myRadMaskedEditBox.MaskType = MaskType.Standard;
myRadMaskedEditBox.Mask = "000000000000";
Hope that clears up the confusion ^_^