You can use the events, it's quite practical and I think it saves you a lot of code.
The event is KeyPress
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsPunctuation(e.KeyChar)) //Comparas si la tecla presionada corresponde a un signo de puntuacion
{
e.Handled = true; //Si coincide se controla el evento, es decir, no se escribe el caracter
}
if (Char.IsSymbol(e.KeyChar)) //Comparas si la tecla presionada corresponde a un simbolo
{
e.Handled = true;
}
if (Char.IsLetter(e.KeyChar)) //Comparas si la tecla presionada corresponde a una letra
{
e.Handled = true;
}
}
The previous code is used to replace the validations that are made comparing the content of TextBox
, since only numbers can be written.
And as for the length of TextBox
there are two ways, one is visually modifying the property MaxLength
and assigning the desired value, in your case 5.
Or the other one is by code:
textBox1.MaxLength = 5; //Si es en WF puedes hacer la declaracion en el metodo Form_Load
If it worked for you, do not forget to mark it!