文本更改事件中的复杂if语句 [英] Complex if-statement in an textchanged event

查看:97
本文介绍了文本更改事件中的复杂if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个复杂的if语句. if语句处于文本框的textchanged事件中.如果if陈述式为true,则应载入pdf档案.问题不在于如何加载PDF文件,那已经可以了,问题在于如何设置if语句.在那里,应查询以下条件:

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:

在位置0处必须为"S", 在位置1处必须为"E", 在位置2处必须为"H", 位置3没关系, 位置4-7代表一个数字,该数字必须为0-3000(不允许超过3000), 位置8处必须再次为"H"或"R"

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"

我使用IndexOf()方法尝试了该方法,它适用于前3个字符,但是与第8个符号相关联,它不再起作用.我认为这与"H"已经存在于位置2有关.

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.

要检查我尝试过的电话号码: Convert.ToInt32(textBox1.Text.Substring(4,4))< = 3000

To check the number I tried it with: Convert.ToInt32(textBox1.Text.Substring(4, 4)) <= 3000

但是那也不起作用.

推荐答案

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

这篇关于文本更改事件中的复杂if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆