将值从文本框添加到标签

时间:2013-12-10 22:16:33

标签: c# .net

我想知道如何将文本框中的值添加到标签中,每次添加另一个Pizza时标签都不会重置为零 我基本上需要代码向我展示如何添加保持向标签添加文本框值

private void SummaryBox_TextChanged(object sender, EventArgs e)//Textbox:the value is shown of the pizza selected
        {

        }

        private void txtPizzaPrice_TextChanged(object sender, EventArgs e)
        {

        }
        Menu(object sender, System.ComponentModel.CancelEventArgs e)
{


        private void lblPrice_Click(object sender, EventArgs e)
        {
            // Label where the Pizza prices needs to be added up each time I press add pizza
        }

    }
}

//HERE ARE THE PIZZA VALUES

      double PizzaPrice;//Global


 double ExtraTopping;//Global

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            string CT;



            if (radioButton2.Enabled == true) //PIZZA CHEESE TOMATO
            {
                double ctp = 3.50;
                PizzaPrice = ctp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice

                CT = radioButton2.Text;

                SummaryBox.Text = CT;


            }
            else
            {
                SummaryBox.Clear();
            }

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            string VS;

            if (radioButton1.Enabled == true) //PIZZA Veg SUpreme
            {
                double vsp = 5.20;
                PizzaPrice = vsp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice

                VS = radioButton1.Text;

                SummaryBox.Text = VS;

            }
            else
            {
                SummaryBox.Clear();
            }

        }

        private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
        {
            string SV;


            if (radioButton3.Enabled == true) //PIZZA SPicy Veg
            {
                double svp = 5.20;
                PizzaPrice = svp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice

                SV = radioButton3.Text;

                SummaryBox.Text = SV;


            }
            else
            {
                SummaryBox.Clear();  
            }
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            string MF;

            if (radioButton6.Enabled == true) //PIZZA Veg SUpreme
            {
                double mfp = 5.80;
                PizzaPrice = mfp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice

                MF = radioButton6.Text;

                SummaryBox.Text = MF;
            }
            else 
            {
                SummaryBox.Clear();
            }
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            string HP;

            if (radioButton7.Enabled == true) //PIZZA Ham pineapple
            {
                double hpp = 4.20;
                PizzaPrice = hpp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
                ^ Value I want to add to lblPrice^

                HP = radioButton7.Text;

                SummaryBox.Text = HP;

            }
            else
            {
                SummaryBox.Clear();

            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            string SF;

            if (radioButton4.Enabled == true) // PIZZA Veg SUpreme
            {
                double sfp = 5.60;
                PizzaPrice = sfp;

                txtPizzaPrice.Text = "£ " + PizzaPrice.ToString(); //Value I want //to add to lblPrice

                SF = radioButton4.Text;

                SummaryBox.Text = SF;

            }
            else
            {
                SummaryBox.Clear();
            }
        }

1 个答案:

答案 0 :(得分:1)

我相信您要做的是向PizzaPrice添加一个值,然后在txtPizzaPrice.Text上显示,并在前面加上£符号。

PizzaPrice应为property rather than a field

public double PizzaPrice { get; set; }

请注意我+=披萨价格的值,然后将其显示在文字属性中:

private void radioButton7_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton7.Checked)
    {
        double hpp = 4.20;
        PizzaPrice += hpp;
        txtPizzaPrice.Text = "£ " + PizzaPrice.ToString();
        SummaryBox.Text = radioButton7.Text;
    }
    else
    {
        SummaryBox.Clear();
    }
}

您可以做很多事情来缩短代码。尝试将披萨类型添加到单选按钮的Tag,并使用结构作为披萨值。但是我会把它留给你。