简体   繁体   中英

Compare two textbox in c#

How can I compare two textboxes in c# using greater than or less than. 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. 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(...)

Update:

Seems, you're not sure about the keyword var . I suggest you have a deep read of the MSDN reference page for var .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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