简体   繁体   中英

C# windows form, how to have radio buttons/checkboxes values input into multiple textboxes?

I am new to C# and have a practice activity I'm really struggling with however am unable to get guidance from my tutor as he is currently ill.

I believe I'm stuck on multiple things so I'll try to be as descriptive as possible..

A screenshot of the application will be linked below. The idea of this application is to create a gym membership system, which allows users to see 'Membership cost', 'Extra charges', 'Total discount', 'Net Membership cost' and 'Regular payment amount', so 5 different calculations are required to be put into the textboxes at the bottom of the application.

These different calculations are done by clicking on separate radio buttons and checkboxes. For example as seen in the picture one radio button may equal '$10' and another may equal '$15'

I wish I had more code to show but I'm really confused so all help is appreciated.

My questions are:

  1. How would I get the application to display 5 different calculations each for a different text box?

  2. How would you get an equation to calculate a percentage discount?

Kind Regards:)

enter image description here

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.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            double results;
            int x, y, z, a, b;

            x = Convert.ToInt16(textbox5.text)
            y = Convert.ToInt16(textbox6.text)
            z = Convert.ToInt16(textbox7.text)
            a = Convert.ToInt16(textbox8.text)
            b = Convert.ToInt16(textbox9.text)

            if (radioButton1.Checked == true)
            {
                value = 10
            }
            else if (radioButton2.Checked == true)
            {
                value = 15
            }
            else if (radioButton3.Checked == true)
            {
                value = 20
            }

            if (radioButton4.Checked == true)
            {
                value = 10
            }
            else if (radioButton5.Checked == true)
            {
                value = 15
            }
            else if (radioButton6.Checked == true)
            {
                value = 20
            }

            if (radioButton7.Checked == true)
            {
                value = 10
            }
            else if (radioButton8.Checked == true)
            {
                value = 15
            }

            if (radioButton9.Checked == true)
            {
                value = 10
            }
            else if (radioButton10.Checked == true)
            {
                value = 15
            }

            if (checkBox1.Checked == true)
            if (checkBox2.Checked == true)
            if (checkBox3.Checked == true)
            if (checkBox4.Checked == true)
        }
    }
}

You need to write logic to populate each of the text boxes at the bottom. In order to do that you have to figure out what goes into each one and store it in a variable. Here are the variables you need to calculate everything.

WeeklyRate =
  if R1 selected : 10
  if R2 selected : 15
  if R3 selected : 20

NumberOfWeeks = 
  if R4 selected : 12
  if R5 selected : 52
  if R6 selected : 104

NumberOfPayments = 
  if R9 selected : NumberOfWeeks
  if R10 selected : NumberOfWeeks * 12/52

MembershipCostTotal = WeeklyRate  * NumberOfWeeks

ExtraCharges = sum of the checked boxes amounts

TotalDiscount = 
  if R4 selected : 0
  if R5 selected : 2 * NumberOfWeeks
  if R6 selected : 5 * NumberOfWeeks

NetMembershipCost = MembershipCostTotal + ExtraCharges - TotalDiscount

RegularPaymentAmount = NetMembershipCost / NumberOfPayments

Once you have calculated the values for all the variables, simply assign the formatted versions of their values to the text properties.

Textbox1.Text = MembershipCostTotal.ToString("C");
Textbox2.Text = ExtraCharges.ToString("C");
Textbox3.Text = (-1 * TotalDiscount).ToString("C");
Textbox4.Text = NetMembershipCost.ToString("C");
Textbox5.Text = RegularPaymentAmount.ToString("C");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM