0

Essentially, I'm trying to check if textbox2.Text contains anything that isn't an integer. This is an incredibly inefficient way of doing it, but it's the only way I could figure out. Is there anything better that I could do?

char[] ca = textBox2.Text.ToCharArray();
foreach (char c in ca)
{
    string s = "1234567890";
    char[] ca2 = s.ToCharArray();
    if (c != ca2[0] && c != ca2[1] && c != ca2[2] && c != ca2[3] && c != ca2[4] &&
        c != ca2[5] && c != ca2[6] && c != ca2[7] && c != ca2[8] && c != ca2[9])
    {
        MessageBox.Show("Non-integer detected in text box.");
        EndInvoke(null);
    }
}
int i = int.Parse(textBox2.Text);
snorepion
  • 25
  • 9
  • Alternatively, could you use a number picker instead of a textbox? That way you would know 100% the value could only be an int and you wouldnt have to do any checks or parsing. – Kyle Rone Jul 12 '17 at 16:32
  • Only integers? Why not just block characters in the first place? – paparazzo Jul 12 '17 at 16:32
  • Pretty sure this would be fairly simple with a Regex. https://stackoverflow.com/questions/273141/regex-for-numbers-only – Aaron Jul 12 '17 at 16:37
  • Good points, but I have it set up to display a watermark if no text is typed in. – snorepion Jul 12 '17 at 16:42
  • @Aaron of course it is, but I didnt want to introduce another problem :D – Jamiec Jul 12 '17 at 16:42

2 Answers2

2

Starting with your code, there is a bunch of things wrong

  1. You're recrerating your array of 0123456789 for every character, you could have done that once outside the loop
  2. There is a method already to check whether a character is a numeric digit - char.IsDigit

char[] ca = textBox2.Text.ToCharArray();
foreach (char c in ca)
{
    if(!char.IsDigit(c))
    {
        MessageBox.Show("Non-integer detected in text box.");
        EndInvoke(null);
    }
}

But you dont even need to do the loop yourself, you can use a Linq method to check Any elements satisfy for a boolean condition

var hasAnyNumbers = textBox2.Text.ToCharArray().Any(char.IsDigit);

But at that point you're trying to parse it to an integer, so I suspect what you meant to ask is "How do I check all the characters are integers" - which is kind of the opposite

var isAllNumbers = textBox2.Text.ToCharArray().All(char.IsDigit);

But, as the other answer states - theres a method for that - int.TryParse.

Jamiec
  • 118,012
  • 12
  • 125
  • 175
  • Thank you. I did make some code based on your answer, but I'm going to switch to TryParse now, since that seems shorter. – snorepion Jul 12 '17 at 16:40
  • @snorepion You dont have to loop over the characters - thats the point of that 1-liner. But defo go with `TryParse` – Jamiec Jul 12 '17 at 16:41
  • As a side-note: .Net `Char.IsDigit` is inconsistent with `SomeNumberType.Parse` implementations. If you'd encounter some decimal digits from outside of the common '0'-'9', `Parse` methods would fail while for all these characters `Char.IsDigit` would evaluate to `true`. – Sergey.quixoticaxis.Ivanov Jul 12 '17 at 16:48
1

Use IsLetter:

bool containNonLetter = textBox2.Text.Any(c => !char.IsLetter(c));

But as your goal is to parse it to an int use instead TryParse:

int result;
if(int.TryParse(textBox2.Text,out result))
{
    //Use result
}
Gilad Green
  • 34,248
  • 6
  • 46
  • 76
  • Thanks. The first one didn't work for me, strangely enough, but I completely forgot about TryParse. That looks like it will be much simpler. – snorepion Jul 12 '17 at 16:43