简体   繁体   中英

How to pre-populate a groupBox in Win Forms C#?

We're doing C# this week. It's the Lunch Order assignment, and the groupBox for my Add-ons menu does not populate until I check another main course. The Add-ons are all Checkboxes inside the groupBox.

I thought I would overcome this by setting the first main course item (hamburger) as checked by default. Did not fix it, and so I thought maybe I should have one of the add-on menu check-boxes checked by default... This also did not improve anything. This is what it looks like when I run the app:

LunchOrderLaunch No add-ons

And when I check another main course option:

LunchOrderMainCourseChange Add-ons show

Here's the code (yes things are out of order because I am new to this and trying to work around things. Apologies for that in advance:

 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 LunchAppLAB1_V2
{
    public partial class frmLunchOrder : Form
    {
        // Global declaration
        decimal Subtotal;
        decimal Tax = 0.05m;
        decimal OrderTotal;

        public frmLunchOrder()
        {
            InitializeComponent();
        }

        private void grbxAddOns_CheckChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            string message = "Hey! Thanks for dining with us today!\n Will you be coming 
            back?";
            DialogResult button =
                MessageBox.Show(message, "Dear Customer",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Information);

            if (button == DialogResult.No)
            {
                this.Close();
            }
            if (button == DialogResult.Yes)
            {
                MessageBox.Show("Hey! Thanks dude! Hope to see you soon!", "Greeting 
                Message");
                this.Close();
            }
        }

        private void btnPlaceOrder_Click(object sender, EventArgs e)
        {
            decimal add = 0m;
            if (chkbx1.Checked)
            {
                add++;
            }
            if (chkbx2.Checked)
            {
                add++;
            }
            if (chkbx3.Checked)
            {
                add++;
            }
            if (rdoHamburger.Checked)
            {
                decimal Hamburger = Convert.ToDecimal(rdoHamburger.Checked);
                Hamburger = 6.95m;

                Subtotal = Hamburger + (add * .75m);
                Tax = Subtotal * 0.05m;
                OrderTotal = Tax + Subtotal;

                txtSubtotal.Text = Subtotal.ToString("c");
                txtTax.Text = Tax.ToString("c");
                txtOrderTotal.Text = OrderTotal.ToString("c");
            }
            else if (rdoPizza.Checked)
            {
                decimal Pizza = Convert.ToDecimal(rdoPizza.Checked);
                Pizza = 5.95m;

                Subtotal = Pizza + (add * .50m);
                Tax = Subtotal * 0.05m;
                OrderTotal = Tax + Subtotal;

                txtSubtotal.Text = Subtotal.ToString("c");
                txtTax.Text = Tax.ToString("c");
                txtOrderTotal.Text = OrderTotal.ToString("c");
            }
            else if (rdoSalad.Checked)
            {
                decimal Salad = Convert.ToDecimal(rdoSalad.Checked);
                Salad = 4.95m;

                Subtotal = Salad + (add * .25m);
                Tax = Subtotal * 0.05m;
                OrderTotal = Tax + Subtotal;

                txtSubtotal.Text = Subtotal.ToString("c");
                txtTax.Text = Tax.ToString("c");
                txtOrderTotal.Text = OrderTotal.ToString("c");
            }
        }

        private void rdoHamburger_CheckedChanged(object sender, EventArgs e)
        {
            grbxAddOns.Text = "Add-on items($.75/each)";
            chkbx1.Text = "Lettuce, Tomato, and Onions";
            chkbx2.Text = "Ketchup, Mustard, and Mayonnaise";
            chkbx3.Text = "French Fries";
        }

        private void rdoPizza_CheckedChanged(object sender, EventArgs e)
        {
            grbxAddOns.Text = "Add-on items($.50/each)";
            chkbx1.Text = "Pepperoni";
            chkbx2.Text = "Sausage";
            chkbx3.Text = "Olives";
        }

        private void rdoSalad_CheckedChanged(object sender, EventArgs e)
        {
            grbxAddOns.Text = "Add-on items($.25/each)";
            chkbx1.Text = "Croutons";
            chkbx2.Text = "Bacon Bits";
            chkbx3.Text = "Bread Sticks";
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            RecursiveClearTextBoxes(this.Controls);
        }

        private void RecursiveClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                    tb.Clear();
                else
                    RecursiveClearTextBoxes(ctrl.Controls);
            }
        }
    }
}



    

All of the radio buttons for the main course option have been coded with a CheckedChanged instruction. So I understand that they don't fire until the button is physically checked (and the checked button changes) So I am wondering how to do this and have the add-ons populate to begin with. I've been trying to find a solution since yesterday, and now I'm pretty much out of time. I'm a C# noob with this assignment due tomorrow...

In the constructor, add this line after InitializeComponent();

public frmLunchOrder()
{
    InitializeComponent();
    rdoHamburger_CheckedChanged(null, null); // <-- this line
}

Your problem is that, even though you're setting one to be clicked by default, it's still not firing the event that sets the check box text.

So you can just call this event manually in the constructor.

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