As I can not comment, I'll use the answer.
I hope I can help you.
I have a WP8 application, in which I had to criticize a Truck's Horse Plate, that is, three letters and four numbers.
After much searching I was able 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 the input individually, if the input is OK the e.Handled = false
, to not accept the input I make the e.handled=true
.
In the KeyDown event, the following code is present:
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;
}
}
}