问题
I want to realize a complex if-statement. The if-statement is in an textchanged event of a textbox. If the if-statement gives true, a pdf-file should be load. The problem is not how to load the PDF-file, thats works already fine, the problem is, how set the if-statement. There, the following conditions should be queried:
At position 0 must be an "S", at position 1 must be an "E", at position 2 must be an "H", position 3 does not matter, position 4-7 represent a number and the number must be from 0-3000 (not allowed to go over 3000), at position 8 must be again an "H" or an "R"
I tried it with the method IndexOf() and it works for the first 3 characters, but in connection with the 8th sign it did not work anymore. I think it is related to the fact that "H" already exists at position 2.
To check the number I tried it with: Convert.ToInt32(textBox1.Text.Substring(4, 4)) <= 3000
But that did not work either.
回答1:
private static bool ShowPdf(string str)
{
if (str[0] != 'S')
return false;
else if (str[1] != 'E')
return false;
else if (str[2] != 'H')
return false;
else if (str[8] != 'H' && str[8] != 'R')
return false;
else if (int.TryParse(str.Substring(4,4), out int number)
return (number >= 0 && number <= 3000);
return true;
}
来源:https://stackoverflow.com/questions/50377714/complex-if-statement-in-an-textchanged-event