检查文本框的范围 [英] Checking range for textboxes

查看:25
本文介绍了检查文本框的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有 2 个文本框,我试图限制输入.以下是我正在尝试做的更详细的描述:

I have 2 textboxes on my form for which I'm trying to restrict the input. Here is a more detailed description of what I'm trying to do:

编写名为 IsValidData 的方法,该方法检查操作数 1 和操作数 2 文本框是否包含介于 0 和 1,000,000(不包括在内)之间的十进制值,以及运算符文本框是否包含有效运算符.

Code a method named IsValidData that checks that the Operand 1 and Operand 2 text boxes contain a decimal value between 0 and 1,000,000 (non-inclusive) and that the Operator text box contains a valid operator.

我知道我这样做的方式是错误的,但我不知道如何解决.我在写if语句的时候想到的方式是这样的:

I know the way that I did this is wrong but I'm not sure how to fix it. The way I thought of it when writing the if statement is this:

检查以确保 txtOperand1 文本框中输入的值是 >0 AND <=100,然后对另一个文本框执行相同操作.有人可以就我做错了什么提出建议吗?谢谢.

check to make sure the value is >0 AND <=100 for the input in the txtOperand1 textbox and then did the same for the other textbox. Can someone please give suggestions on what I'm doing wrong? Thanks.

double operand1 = Convert.ToDouble(txtOperand1.Text);
double operand2 = Convert.ToDouble(txtOperand2);


        if ((operand1 > 0 && operand1 <= 100) &&
            (operand2 > 0 && operand2 <= 100))
            return true;

推荐答案

这样的事情?

return decimal.TryParse(txt.Text, out decimal val) && (val > 0m & val < 1000000m)

这篇关于检查文本框的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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