Complex if-statement in an textchanged event

寵の児 提交于 2019-12-11 07:29:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!