简体   繁体   English

比较C#中的两个文本框

[英]Compare two textbox in c#

How can I compare two textboxes in c# using greater than or less than. 如何使用大于或小于比较c#中的两个文本框。 Im working on billing system. 我在计费系统上工作。 I want the cash shoould be bigger than the total. 我希望现金应该大于总数。 if not. 如果不。 it will pop up messagebox for error. 它将弹出错误消息框。 I already try convert to int. 我已经尝试将其转换为int。 to double. 加倍。 to string. 串起来。 Here's my current code right now. 这是我当前的代码。

if (txtCash.TextLength < txtTotal.TextLength){
    MessageBox.Show("Cash must me bigger than the        total");
    txtCash.Focus();
    return;
  }

Any one can help me how? 谁能帮助我? I know text length is wrong. 我知道文字长度错误。 but im just using it. 但我只是用它。

I'm working on billing system. 我正在开发计费系统。 I want the cash should be bigger than the total. 我希望现金应该大于总数。 if not. 如果不。 it will pop up message box for error. 它将弹出错误消息框。

You might be looking for something like this: 您可能正在寻找这样的东西:

if (double.Parse(txtCash.TextLength) <= double.Parse(txtTotal.TextLength))
{
    MessageBox.Show("Cash must me bigger than the total");
    txtCash.Focus();
    return; // i dont know why you put this here but i'll leave it there anyway.
}
else
{
    var amount = double.Parse(txtCash.TextLength);
    // DO something
}

You may want to further validate the user input if you wish. 如果需要,您可能希望进一步验证用户输入。 Example, if the user doesn't enter numbers... 例如,如果用户未输入数字...


Better ways to achieve your task in the most robust way possible, you should consider the below links: 更好的方法以尽可能强大的方式完成任务,您应该考虑以下链接:

How do I make a textbox that only accepts numbers? 如何制作仅接受数字的文本框?

double.TryParse(...) double.TryParse(...)

Update: 更新:

Seems, you're not sure about the keyword var . 似乎您不确定关键字var I suggest you have a deep read of the MSDN reference page for var . 我建议您深入阅读var的MSDN参考页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM