开发者

How can I make this program check for multiple things one-at-a-time?

开发者 https://www.devze.com 2023-04-12 09:02 出处:网络
/At the bottom of this code there are two if statements. I want to be able to check FIRST whether the user has selected a flavor(the text for the flavor textbox is \"Select a flavor\" by default). If

/At the bottom of this code there are two if statements. I want to be able to check FIRST whether the user has selected a flavor(the text for the flavor textbox is "Select a flavor" by default). If they click the AddDrink button without selecting a flavor, the message box needs to show that they haven't selected a flavor, instead of haven't entered a quantity, and then proceed with the program. However, if they have chosen a flavor but haven't entered a quantity then it should show the message box about entering a quantity. If anymore info is needed please let me know. THANKS FOR YOUR HELP!!!/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Chap9DrinkApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ComputeCost_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnCompleteOrder_Click(object sender, EventArgs e)
        {
            MessageBox.Show(lblFinalFlavor.Text + "\n" + lblFinalTopping.Text + "\n" + lblFinalCost.Text);
        }

        private void btnAddDrink_Click(object sender, EventArgs e)
        {
            double cost = 0;
            double quantityPrice;
            double quantityNum;
            this.lblFinalTopping.Text = "Extras: ";

            if (this.radSmall.Checked)
            {
                cost += 3.00;
            }
            else if (this.radMedium.Checked)
            {
                cost += 3.50;
            }
            else if (this.radLarge.Checked)
            {
                cost += 4.00;
            }

            if (this.ckBoxCherries.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Cherries ";
            }
            if (this.ckBoxGrape.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Grapes ";
            }
            if (this.ckBoxLemon.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Lemon ";
            }
            if (this.ckBoxPineapple.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Pineapple ";
            }
            if (this.ckBoxStrawberry.Checked)
            {
                cost += .5开发者_如何学C0;
                this.lblFinalTopping.Text += "Strawberry ";
            }
            if (this.ckBoxVitamin.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Vitamin Pack ";
            }
            if (this.ckBoxWhipped.Checked)
            {
                cost += .50;
                this.lblFinalTopping.Text += "Whipped cream ";
            }

            this.lblFinalCost.Visible = true;            

            if (this.txtQuantity.Text != "0")
            {
                quantityNum = double.Parse(this.txtQuantity.Text);
                quantityPrice = cost * quantityNum;
                this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
            }
            else
                MessageBox.Show("Please enter a quantity!");

            if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
                this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
            else
                MessageBox.Show("Please select a flavor!");
        }
    }
}


Isn't this what you want:

if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
{
    this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
    if (this.txtQuantity.Text != "0")
    {
        quantityNum = double.Parse(this.txtQuantity.Text);
        quantityPrice = cost * quantityNum;
        this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
    }
    else
    {
        MessageBox.Show("Please enter a quantity!");
    }
}
else
{
    MessageBox.Show("Please select a flavor!");
}


There's better ways to do what you're trying to do. But just to get your problem solved, you could try this -

.
.
.
   string msgBoxTxt = "";

    if (this.txtQuantity.Text != "0")
    {
        quantityNum = double.Parse(this.txtQuantity.Text);
        quantityPrice = cost * quantityNum;
        this.lblFinalCost.Text = "Cost: " + quantityPrice.ToString("c");
    }
    else
        msgBoxTxt += "Please enter a quantity! ";

   if (this.cmboBoxJuiceFlav.Text != "Select a flavor")
       this.lblFinalFlavor.Text = "Flavor: " + this.cmboBoxJuiceFlav.Text;
   else
       msgBoxTxt += "Please select a flavor!";

   if (msgBoxTxt != "")
        MessageBox.Show(msgBoxTxt);
0

精彩评论

暂无评论...
验证码 换一张
取 消