As I cannot comment, so I will use the answer.
I hope I can help you.
I have a WP8 application, in which I had to criticize the Horse Plate of a Truck, that is, three letters and four numbers.
After a lot of research I managed to solve the problem as follows:
**<TextBox x:Name="txtCavalo" Canvas.Top="15" Canvas.Left="0" Width="180" Text="" InputScope="Text" KeyDown="txtCavaloKeyDown" />**
With the Keydown event it is possible to determine individually the input, if the input is OK the e.Handled = false
, not to accept the input do the e.handled=true
.
In the Keydown event, there is the following code:
private void txtCavaloKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (txtCavalo.Text.Length > 2)
{
if (txtCavalo.Text.Length < 7)
{
if ((e.Key >= Key.D0 && e.Key <= Key.D9) | (e.Key == Key.Enter | e.Key == Key.Back | e.Key == Key.Shift))
{
e.Handled = false;
}
else
{
MessageBox.Show("Placa Cavalo - Informe 3 letras e 4 números.", "Atenção", MessageBoxButton.OK);
e.Handled = true;
}
}
else
{
if (e.Key == Key.Back | e.Key == Key.Shift | e.Key == Key.Enter)
{
e.Handled = false;
}
else
{
MessageBox.Show("Placa Cavalo - Informe 3 letras e 4 números.", "Atenção", MessageBoxButton.OK);
e.Handled = true;
}
}
}
else
{
if ((e.Key == Key.A | e.Key == Key.B | e.Key == Key.C | e.Key == Key.D | e.Key == Key.E | e.Key == Key.F | e.Key == Key.G | e.Key == Key.H
| e.Key == Key.I | e.Key == Key.J | e.Key == Key.K | e.Key == Key.L | e.Key == Key.M | e.Key == Key.N | e.Key == Key.O | e.Key == Key.P | e.Key == Key.Q
| e.Key == Key.R | e.Key == Key.S | e.Key == Key.T | e.Key == Key.U | e.Key == Key.V | e.Key == Key.W | e.Key == Key.X | e.Key == Key.Y | e.Key == Key.Z)
| (e.Key == Key.Enter | e.Key == Key.Back | e.Key == Key.Shift))
{
e.Handled = false;
}
else
{
MessageBox.Show("Placa Cavalo - Informe 3 letras e 4 números.", "Atenção", MessageBoxButton.OK);
e.Handled = true;
}
}
}
+1 Just to comment, an alternative would be
if(! char.IsDigit(le))
.– Bacco
Thanks for the tip!
– Leonardo