Hello I’m a little stuck on a task involving radio buttons. I am using an else… if statement to determine which radio boxes are checked and I then want a different calculation to be done depending on what radio button is selected from both of the group boxes. In one group box I have 3 options which are items of clothing and the price and in the other group box I have 4 options which are quantities 1-4. When two radio buttons are selected (one from each group box) I would like a calculation to take place. I think my if statements are correct but i’m having trouble doing the calculation and converting it to string as i’ve only ever done calculations using txtboxes which i then convert to int or double and involve them in the calculation. since I’m not using a textbox on this form i’m stumped here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Radio_Button_task
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool blnChecked = true;
private void btnEnd_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnClear_Click(object sender, EventArgs e)
{
lblTotal.Text = "";
rbtnJeans.Checked = false;
rbtnQuant1.Checked = false;
rbtnQuant2.Checked = false;
rbtnQuant3.Checked = false;
rbtnQuant4.Checked = false;
rbtnShirt.Checked = false;
rbtnShorts.Checked = false;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
double dbTotal;
if (rbtnJeans.Checked && rbtnQuant1.Checked)
{
dbTotal = Convert.ToDouble(28 * 1);
}
else if (rbtnJeans.Checked && rbtnQuant2.Checked)
{
dbTotal = Convert.ToDouble(28 * 2);
}
else if (rbtnJeans.Checked && rbtnQuant3.Checked)
{
dbTotal = Convert.ToDouble(28 * 3);
}
else if (rbtnJeans.Checked && rbtnQuant4.Checked)
{
dbTotal = Convert.ToDouble(28 * 4);
}
lblTotal.Text = "The total cost is: £" + dbTotal.ToString("0.00");
}
}
}
You are not handling the case where no radio buttons or only one is checked. Initialize dbTotal to zero.