Plato
0
Q:

c# check if string is all numbers

string s1 = "123";
string s2 = "abc";

bool isNumber = int.TryParse(s1, out int n); // returns true
isNumber = int.TryParse(s2, out int n); // returns false
3
if (textVar.All(c => Char.IsLetterOrDigit(c))) {
    // String contains only letters & numbers
}
2
if (str.All(char.IsDigit)) {
  // String only contains numbers
}
0
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // Verify that the pressed key isn't CTRL or any non-numeric digit
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
    {
        e.Handled = true;
    }

    // If you want, you can allow decimal (float) numbers
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
}
0

New to Communities?

Join the community