How to identify if user is typing number or characters?

Asked

Viewed 3,226 times

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, but I want to treat this in the code, because I have two lists, one with name and one with number. Then how to 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 no longer works..

In short:

When the user type: 123 it makes a search in the list of numbers.

When the user type: abc he does a search on the list of names.

3 answers

1


I got here implementing the following method:

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;
} 
  • 3

    +1 Just to comment, an alternative would be if(! char.IsDigit(le)).

  • Thanks for the tip!

0

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;
        }
    }
}

0

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

int n;
bool ehUmNumero = int.TryParse(TextBox.Text, out n);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.