Member 13054756 Ответов: 1

Вопрос C# для VS 2015. Как реализовать if else if


Я делаю простую программу, которая является классным заданием. Я не могу понять,как обернуть код.

Что я уже пробовал:

public partial class frmPetClinic : Form
    {

 public frmPetClinic()
        {
            InitializeComponent();

            // Delcare varibles.

            Boolean bolFleaTreatment;
            Boolean bolHairTrim;
            Boolean bolNailTrim;
            Boolean bolDematting;
            Boolean bolHeartwormTreatmen;
            Boolean bolHepatitisVaccine;
            Boolean bolDistemperVaccine;
            Boolean bolRabiesVaccine;

            double dblCost;

            bolFleaTreatment = chkFleaTreatment.Checked;
            bolHairTrim = chkHairTrim.Checked;
            bolNailTrim = chkNailTrim.Checked;
            bolDematting = chkDematting.Checked;
            bolHeartwormTreatmen = chkHeartwormTreatment.Checked;
            bolHepatitisVaccine = chkHepatitisVaccine.Checked;
            bolDistemperVaccine = chkDistemperVaccine.Checked;
            bolRabiesVaccine = chkRabiesVaccine.Checked;

            dblCost = 0;

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //Exit the app.

            Application.Exit();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //Clear selected checkboxes.
            //Clear running total.

            chkFleaTreatment.Checked = false;
            chkHairTrim.Checked = false;
            chkNailTrim.Checked = false;
            chkDematting.Checked = false;
            chkHeartwormTreatment.Checked = false;
            chkHepatitisVaccine.Checked = false;
            chkDistemperVaccine.Checked = false;
            chkRabiesVaccine.Checked = false;

            lblCost.Text = string.Empty;
        }

        private void chkFleaTreatment_CheckedChanged(object sender, EventArgs e)
        {

            // Decide which if any checkboxes are checked.
            // Add total costs and convert to currency

            // If box is unchecked, deduct price from cost
            if
                (true)

                
            else
                
        }
          

    }
        
}

1 Ответов

Рейтинг:
1

Bryian Tan

основываясь на коде, который вы представили, что-то вроде этого должно сделать трюк. Как только все заработает, подумайте о том, как его улучшить или сделать короче.

public partial class frmPetClinic : Form
    {
double currentTotal = 0;
        double fleaTreatment = 112.5;
        double hairTrim = 88;
        System.Globalization.NumberStyles style =   System.Globalization.NumberStyles.Number | 
                                                    System.Globalization.NumberStyles.AllowCurrencySymbol;
        System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

        private void chkFleaTreatment_CheckedChanged(object sender, EventArgs e)
        {
            
            double.TryParse(lblCost.Text, style, culture, out currentTotal);

            if (chkFleaTreatment.Checked)
            {
                lblCost.Text = Math.Round(currentTotal + fleaTreatment, 2).ToString("C2");
            }
            else
            {
                lblCost.Text = Math.Round(currentTotal - fleaTreatment, 2).ToString("C2");
            }
        }

        private void chkHairTrim_CheckedChanged(object sender, EventArgs e)
        {
            double.TryParse(lblCost.Text, style, culture, out currentTotal);

            if (chkHairTrim.Checked)
            {
                lblCost.Text = Math.Round(currentTotal + hairTrim, 2).ToString("C2");
            }
            else
            {
                lblCost.Text = Math.Round(currentTotal - hairTrim, 2).ToString("C2");
            }
        }

//private void chkNailTrim_CheckedChanged(object sender, EventArgs e)
//...

}