当我在C#中比较两个文本框值时,我收到错误 [英] I am getting error when I am compare two textbox values in C#

查看:85
本文介绍了当我在C#中比较两个文本框值时,我收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我在比较c#\

中的两个文本框值时收到错误错误输入字符串格式不正确

请帮帮我并告诉我解决方法



我尝试了什么:



Hello Guys! i am getting an error when i am compare two textbox values in c# \
the error is input string was not in correct format
please help me and tell me the solution

What I have tried:

if (int.TryParse(textBox00.Text) > int.TryParse(txtTotal.Text))
                        {
                            MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {

推荐答案

嗨会员9983063,



这不是 TryParse()的方式。 TryParse()需要两个参数 - 一个是要转换为整数的文本,第二个是存储转换值的输出变量。您提供的代码转换为:

Hi Member 9983063,

This is not the way TryParse() works. TryParse() requires two parameters - one is the text to convert to integer, and the second is the output variable where it stores the converted value. The code you provided translates into:
if (Boolean > Boolean)     // You are trying to compare booleans.
{
    MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

这本身是错误的,因为布尔值只能与'=='和'!='的另一个布尔值进行比较。从逻辑上讲,你不能说'如果真是大于假'。所以这是另一个编译错误以及 TryParse()的其他编译错误。



你可以试试以下方法:

This itself is wrong as a boolean can be compared with another boolean for '==' and '!=' only. Logically you cannot say 'if true is greater than false'. So this is one more compilation error along with the other compile error for TryParse().

You can try the following approach:

int textBox00Val, txtTotalVal;
if (int.TryParse(textBox00.Text, out textBox00Val) == false)    // This line checks if there is an integer in the box, and also converts it to an integer.
{
    MessageBox.Show("textBox00 value must be anumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

if (int.TryParse(txtTotal.Text, out txtTotalVal) == false)      // This line checks if there is an integer in the box, and also converts it to an integer.
{
    MessageBox.Show("txtTotal value must be anumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

if (textBox00Val > txtTotalVal)     // Now you need to compare the integers.
{
    MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}
............
............

这里发生的是,你获取整数值,然后比较整数。



@ cvogt61457:我想会员9983063意味着编译错误,因为有问题的代码永远不会编译( TryParse()必须提供两个参数,并且布尔值不能与大于运算符的另一个布尔值进行比较。

What happens here is, you obtain the values in integers and then compare the integers.

@cvogt61457: I guess Member 9983063 meant a compile error, as the code in question will never compile (TryParse() must be provided two parameters, and a boolean cannot be compared to another boolean for 'greater than' operator).


嗨试试这个



Hi try this

 int textBox00 = 0;
 int txtTotal = 0;

if(int.TryParse(textBox00.Text, out textBox00) && int.TryParse(txtTotal.Text,out txtTotal))
 {
     if (textBox00 > txtTotal)
     {
         MessageBox.Show("Amount is less than bill amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }else
     {

     }
 }else
 {
     //Conversion error
     MessageBox.Show("no valid number found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

 }


这篇关于当我在C#中比较两个文本框值时,我收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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