Question add the values of the selected radio button?

pls3399

New member
Joined
Oct 10, 2012
Messages
1
Programming Experience
Beginner
Hello all,

I am a Programming student and am having trouble with my first C# windows forms (Could do it in VB). If I am breaking any forum rules by asking for help I am sorry and please delete.

The code I am having trouble with is:
        int OneScoop;
        int TwoScoop;
        int ThreeScoop;
        int Cherry;
        int Nuts;
        int Fudge;
        int ToppingsSum;
        int ScoopSum;


        public void grpNumberofScoops_Enter(object sender, EventArgs e)
        {
            if (radOne.Checked == true)
                OneScoop = 1000;
            else OneScoop = 0;
            
            if (radOne.Checked == true)
                lblCalCount.Visible = true;
            else lblCalCount.Visible = false;


            if (radTwo.Checked == true)
                TwoScoop = 2000;
            else TwoScoop = 0;
            
            if (radTwo.Checked == true)
                lblCalCount.Visible = true;
            else lblCalCount.Visible = false;


            if (radThree.Checked == true)
                ThreeScoop = 3000;
            else ThreeScoop = 0;
          
            if (radThree.Checked == true)
                lblCalCount.Visible = true;
            else lblCalCount.Visible = false;


            ScoopSum = OneScoop + TwoScoop + ThreeScoop;
        }


        public void grpToppings_Enter(object sender, EventArgs e)
        {
            if (chkCherry.Checked == true)
                Cherry = 100;
            else Cherry = 0;
            if (chkNuts.Checked == true)
                Nuts = 200;
            else Nuts = 0;
            if (chkFudge.Checked == true)
                Fudge = 300;
            else Fudge = 0;


            ToppingsSum = Cherry + Nuts + Fudge;
         }


        


        public void lblCalCount_Click(object sender, EventArgs e)
        {
            int Sum = ToppingsSum + ScoopSum;


            if (Sum >= 2500)
                lblCalCount.ForeColor = Color.Red;


            lblCalCount.Text = Convert.ToString(Sum);
        }


It is supposed to add the values of the selected radio button (radOne = 1000, radTwo = 2000, radThree = 3000) and the check box (chkCherry = 100, chkNuts = 200, chkFudge = 300) and show that sum in lblCalCount. It is not adding properly and most of the time the label stays at 0. I am not asking for someone to fix it for me, but if you could point me in the right direction, that would be great. Thank you.
 
Last edited by a moderator:
You should not be handling the Enter events of the GroupBoxes. You should be handling the CheckedChanged events of the RadioButtons and CheckBoxes. You only need one event handler for all three RadioButtons but you would have one each for the CheckBoxes. Note that the CheckedChanged event is raised when a control is checked or unchecked, so you'll have to test its state to determine what to do.

Also, it doesn't make sense to have three variables for the number of scoops. The whole point of RadioButtons is that only one of them can be checked at a time, so you should just one variable and set it to either 1000, 2000 or 3000.
 
Back
Top Bottom