简体   繁体   中英

Using the Selected Item from Combo box in If Statement

Basically i have a vending machine and the total price will be displayed in a Label. Now there is a combo box showing money for example 1.00 , 2.00 , 5.00 etc.

Now i want to check with an if statement weather the money that is chosen from the combo box is bigger or smaller from the total price.

the code is not working

if (cbMoney.SelectedItem < total)
{
   MessageBox.Show("Not Enough Money");
}

ERROR Description :Operator '<' cannot be applied to operands of type 'object' and 'double'

SelectedItem is an object and will need to be parsed.

if (double.Parse(cbMoney.SelectedItem.ToSTring()) < total)
{
    MessageBox.Show("Not Enough Money");
}

http://msdn.microsoft.com/en-us/library/system.double.parse(v=vs.110).aspx

how do you guys know the total type?

if (Convert.ToDouble(cbMoney.SelectedItem.ToString()) < Convert.ToDouble(total))
{
  MessageBox.Show("Not Enough Money");
}

you are taking the SelectedItem object and not converting before checking.

Try as below:

if (Convert.ToDouble(cbMoney.SelectedItem.ToString()) < total)
{
  MessageBox.Show("Not Enough Money");
}

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