【问题标题】:How to Correctly Bind Properties to Form Events?如何正确地将属性绑定到表单事件?
【发布时间】:2025-12-30 14:45:11
【问题描述】:

我正在编写 HeadFirst C# Book 中的教程。表单本身以前可以工作,但现在不行,我不记得我改变了什么。我现在已经多次阅读文本,它似乎是逐字复制,但它不起作用。我究竟做错了什么?我想问题一定出在 Form.cs 文件中。

这是课程的代码。

    class DinnerParty
    {
        public const int CostOfFoodPerPerson = 25;

        //declare DinnerParty automatic properties
        public int NumberOfPeople { get; set; }
        public bool HealthyOption { get; set; }
        public bool FancyDecorations { get; set; }

        // Setup Object Constructor with parameters masking the properties
        public DinnerParty(int numberOfPeople, bool healthyOption, bool fancyDecorations)
        {
            //Set Property Values to parameter values
            NumberOfPeople = numberOfPeople;
            HealthyOption = healthyOption;
            FancyDecorations = fancyDecorations;


        }

        //  Use private methods to access public properties bound to the form.

        private decimal CalculateCostOfBeveragesPerPerson()
        {
            decimal costOfBeveragesPerPerson;
            if (HealthyOption)
            {
                costOfBeveragesPerPerson = 5.00M;
            }
            else
            {
                costOfBeveragesPerPerson = 20.00M;
            }
            return costOfBeveragesPerPerson;
        }


        protected decimal CalculateCostOfDecorations()
        {
            decimal costOfDecorations;
            if (FancyDecorations)
            {
                costOfDecorations = (NumberOfPeople * 15.00M) + 50M;
            }
            else
            {
                costOfDecorations = (NumberOfPeople * 7.50M) + 30M;
            }
            return costOfDecorations;
        }

        //declare read only Cost property to be bound to costLabel control
        public decimal Cost
        {
            get
            {
                decimal totalCost = CalculateCostOfDecorations();
                totalCost += ((CalculateCostOfBeveragesPerPerson() + CostOfFoodPerPerson) * NumberOfPeople);
                if (HealthyOption)
                {
                    totalCost *= .95M;
                }
                return totalCost;
            }
        }
    }
}

这是表单的代码。

namespace DinnerParty
{
    public partial class Form1 : Form
    {
        DinnerParty dinnerParty;

        public Form1()
        {
            InitializeComponent();
            //Initialize DinnerParty object. Initialize DinnerParty party with      default form values.
            dinnerParty = new DinnerParty((int) numericUpDown1.Value, healthyBox.Checked , fancyBox.Checked);
            DisplayDinnerPartyCost();
        }

        // Bind form controls to DinnerParty Properties
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
           dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
            DisplayDinnerPartyCost();
        }

        private void fancyBox_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.FancyDecorations = fancyBox.Checked;
            DisplayDinnerPartyCost();

        } 

        private void healthyBox_CheckedChanged(object sender, EventArgs e)
        {
            dinnerParty.HealthyOption = healthyBox.Checked;
            DisplayDinnerPartyCost();
        }

        private void DisplayDinnerPartyCost()
        {
            decimal Cost = dinnerParty.Cost;
            costLabel.Text = Cost.ToString("c");
        }
    }
}

【问题讨论】:

  • 此问题不符合本网站的指南。具体是什么不起作用? FAQ
  • 我更新了问题。这更清楚吗?对不起。
  • 如果您要复制代码,请确保在设计器中再次连接事件处理程序,或者手动连接,healthyBox.CheckedChanged += healthyBox_CheckedChanged;
  • 谢谢!!!这行得通。我知道它必须是简单的!太感谢了!我永远不会找到这个大声笑。

标签: c# .net winforms


【解决方案1】:

您的代码中的以下行可能是错误的。我没有看到任何课程 DinnerParty2。将其更改为 DinnerParty

 dinnerParty = new DinnerParty2((int) numericUpDown1.Value,   healthyBox.Checked , fancyBox.Checked);

【讨论】:

  • 对不起,我以为我把它们都改回来了。我试图弄清楚,所以我只是将同一个文件复制到一个新表单中并将其命名为 DinnerParty2。我只是改变了它,所以它反映了原来的。它编译正确,没有错误。
  • 谢谢。如果我的堆栈溢出成员提供的任何解决方案对您有帮助,请接受该解决方案。您可以通过单击答案附近的刻度线来做到这一点。
  • 问题不是编译错误,我在编译时从来没有遇到过问题。该问题的解决方案实际上是由 LarsTech 在上面发布的,但他没有将其作为“答案”提交,所以我无法将他标记为正确。也许你可以,我不知道。不过我很感谢你的贡献。
【解决方案2】:

根据 cmets,如果您要复制代码,请确保在设计器中重新连接事件处理程序,或者手动连接:

healthyBox.CheckedChanged += healthyBox_CheckedChanged;

因为当您复制和粘贴控件时,事件处理程序不包括在内。

【讨论】: