winforms 如何在Win Forms C#中预填充groupBox?

llycmphe  于 2022-12-27  发布在  C#
关注(0)|答案(1)|浏览(152)

这周我们在做C#。这是午餐订单任务,我的附加组件菜单的groupBox直到我选中另一个主菜时才会填充。附加组件都是groupBox中的复选框。
我想我可以通过设置第一道主菜(汉堡包)为默认选中来克服这个问题。没有解决这个问题,所以我想也许我应该让一个附加菜单复选框为默认选中...这也没有改善任何东西。这是我运行应用程序时的样子:
LunchOrderLaunch No add-ons
而当我检查另一道主菜选项时:
LunchOrderMainCourseChange Add-ons show
下面是代码(是的,事情有点乱,因为我是新手,正在尝试解决一些问题。事先向您道歉:

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);
            }
        }
    }
}

主菜选项的所有单选按钮都用CheckedChanged指令编码,所以我知道只有在按钮被物理选中后,它们才会触发(和选中的按钮变化)所以我想知道如何做到这一点,并有加载项填充开始。我一直试图找到一个解决方案,因为昨天,现在我几乎没有时间了。我是一个C#新手,明天要交作业...

kkih6yb8

kkih6yb81#

在构造函数中,在 * InitializeComponent()之后 * 添加此行;

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

您的问题是,即使您将一个复选框设置为默认单击,它仍然没有触发设置复选框文本的事件。
因此,您可以在构造函数中手动调用此事件。

相关问题