How to identify if user is typing number or characters?

4

I have a textbox:

<TextBox GotFocus="buscador_GotFocus" Tap="buscador_Tap" KeyUp="buscador_KeyUp" TextChanged="buscador_TextChanged" Name="buscador" Grid.Row="0" DataContext="Teste"/>

In it the user will be able to type any type of character, however I want to handle this in the code, because I have two lists, one with a name and one with a number. So how do you know if the user is typing in the letter or number textbox? I used if comparing the first character only, but after the second character it no longer works ..

In short:

When the user types: 123 he searches the list of numbers.

When the user types: abc he searches the list of names.

    
asked by anonymous 15.11.2015 / 18:34

3 answers

1

I've been able to implement the following method here:

public bool VerificaString(string str)
{
    char[] c = str.ToCharArray();
    char le = ' ';
    for(int cont = 0; cont < c.Length; cont ++)
    {
        le = c[cont];
        if(char.IsLetter(le) || char.IsPunctuation(le))
            return true;
    }
    return false;
} 
    
15.11.2015 / 19:03
0

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;
        }
    }
}
    
16.11.2015 / 18:42
0

To identify whether the character you typed is a number or not you can use the TryParse of int

int n;
bool ehUmNumero = int.TryParse(TextBox.Text, out n);
    
16.11.2015 / 19:44