CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2007
    Posts
    11

    CheckBox problem

    I have created a form with 2 checkboxes but the first one is not working.
    I post the code.

    DinnerParty.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Dinner
    {
        public class DinnerParty
        {
            public const int CostOfFoodPerPerson = 25;
            public int NumberOfPeople;
            public decimal CostOfBeveragesPerPerson;
            public decimal CostOfDecorations = 0;
    
            public void SetHealthyOption(bool healthyOption)
            {
                if (healthyOption)
                {
                    CostOfBeveragesPerPerson = 5.00M;
                }
                else
                {
                    CostOfBeveragesPerPerson = 20.00M;
                }
            }
    
            public void CalculateCostOfDecorations(bool fancy)
            {
                if (fancy)
                {
                    CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
                }
                else
                {
                    CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
                }
            }
    
            public decimal CalculateCost(bool healthyOption)
            {
                decimal totalCost = CostOfDecorations +
                    ((CostOfBeveragesPerPerson + CostOfFoodPerPerson) * NumberOfPeople);
    
                if (healthyOption)
                {
                    return totalCost * .95M;
                }
                else
                {
                    return totalCost;
                }
            }
    
            
        }
    }

    Form1.cs
    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 Dinner
    {
        public partial class Form1 : Form
        {
            DinnerParty dinnerParty;
            public Form1()
            {
                InitializeComponent();
                dinnerParty = new DinnerParty() { NumberOfPeople = 5 };
                dinnerParty.CalculateCostOfDecorations(fancyBox.Checked);
                dinnerParty.SetHealthyOption(healthyBox.Checked);
                DisplayDinnerPartyCost();
            }
    
            private void DisplayDinnerPartyCost()
            {
                decimal Cost = dinnerParty.CalculateCost(healthyBox.Checked);
                costLabel.Text = Cost.ToString("c");
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                dinnerParty.NumberOfPeople = (int)numericUpDown1.Value;
                DisplayDinnerPartyCost();
            }
    
            private void fancyBox_CheckedChanged(object sender, EventArgs e)
            {
                dinnerParty.CalculateCostOfDecorations(fancyBox.Checked);
                DisplayDinnerPartyCost();
            }
    
            private void healthyBox_CheckedChanged(object sender, EventArgs e)
            {
                dinnerParty.SetHealthyOption(healthyBox.Checked);
                DisplayDinnerPartyCost();
            }
    
    
    
    
    
    
        }
    }

    Form1.Designer.cs
    Code:
    namespace Dinner
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
                this.label1 = new System.Windows.Forms.Label();
                this.fancyBox = new System.Windows.Forms.CheckBox();
                this.healthyBox = new System.Windows.Forms.CheckBox();
                this.label2 = new System.Windows.Forms.Label();
                this.costLabel = new System.Windows.Forms.Label();
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
                this.SuspendLayout();
                // 
                // numericUpDown1
                // 
                this.numericUpDown1.Location = new System.Drawing.Point(100, 58);
                this.numericUpDown1.Maximum = new decimal(new int[] {
                20,
                0,
                0,
                0});
                this.numericUpDown1.Minimum = new decimal(new int[] {
                1,
                0,
                0,
                0});
                this.numericUpDown1.Name = "numericUpDown1";
                this.numericUpDown1.Size = new System.Drawing.Size(120, 20);
                this.numericUpDown1.TabIndex = 0;
                this.numericUpDown1.Value = new decimal(new int[] {
                5,
                0,
                0,
                0});
                this.numericUpDown1.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(110, 32);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(92, 13);
                this.label1.TabIndex = 1;
                this.label1.Text = "Number of People";
                // 
                // fancyBox
                // 
                this.fancyBox.AutoSize = true;
                this.fancyBox.Checked = true;
                this.fancyBox.CheckState = System.Windows.Forms.CheckState.Checked;
                this.fancyBox.Location = new System.Drawing.Point(100, 127);
                this.fancyBox.Name = "fancyBox";
                this.fancyBox.Size = new System.Drawing.Size(113, 17);
                this.fancyBox.TabIndex = 2;
                this.fancyBox.Text = "Fancy decorations";
                this.fancyBox.UseVisualStyleBackColor = true;
                // 
                // healthyBox
                // 
                this.healthyBox.AutoSize = true;
                this.healthyBox.Location = new System.Drawing.Point(100, 168);
                this.healthyBox.Name = "healthyBox";
                this.healthyBox.Size = new System.Drawing.Size(96, 17);
                this.healthyBox.TabIndex = 3;
                this.healthyBox.Text = "Healthy Option";
                this.healthyBox.UseVisualStyleBackColor = true;
                this.healthyBox.CheckedChanged += new System.EventHandler(this.healthyBox_CheckedChanged);
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(97, 274);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(28, 13);
                this.label2.TabIndex = 4;
                this.label2.Text = "Cost";
                // 
                // costLabel
                // 
                this.costLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                this.costLabel.Location = new System.Drawing.Point(185, 274);
                this.costLabel.Name = "costLabel";
                this.costLabel.Size = new System.Drawing.Size(100, 23);
                this.costLabel.TabIndex = 5;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(498, 352);
                this.Controls.Add(this.costLabel);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.healthyBox);
                this.Controls.Add(this.fancyBox);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.numericUpDown1);
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "Form1";
                this.Text = "Party Planner";
                ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.NumericUpDown numericUpDown1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.CheckBox fancyBox;
            private System.Windows.Forms.CheckBox healthyBox;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label costLabel;
        }
    }

    Program.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace Dinner
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }

  2. #2
    Join Date
    Sep 2007
    Posts
    82

    Re: CheckBox problem

    what isn't working with it???

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: CheckBox problem

    In the Form1.Designer.cs, "fancyBox" checkbox does not have the CheckedChanged handler.

    You are missing this...
    Code:
    this.fancyBox.CheckedChanged += new System.EventHandler(this.fancyBox_CheckedChanged);
    try this..
    Code:
    // 
    // fancyBox
    // 
    this.fancyBox.AutoSize = true;
    this.fancyBox.Checked = true;
    this.fancyBox.CheckState = System.Windows.Forms.CheckState.Checked;
    this.fancyBox.Location = new System.Drawing.Point(100, 127);
    this.fancyBox.Name = "fancyBox";
    this.fancyBox.Size = new System.Drawing.Size(113, 17);
    this.fancyBox.TabIndex = 2;
    this.fancyBox.Text = "Fancy decorations";
    this.fancyBox.UseVisualStyleBackColor = true;
    this.fancyBox.CheckedChanged += new System.EventHandler (this.fancyBox_CheckedChanged);

  4. #4
    Join Date
    Nov 2007
    Posts
    110

    Re: CheckBox problem

    The fancybox isn't working because you haven't hooked the checkchanged event to it.

    In the designer, select the fancybox, go to the properties window and click the lightning bolt. Find the checkChanged event and in the dropdown select fancyBox_CheckedChanged


    Edit:
    Sniped =-(

  5. #5
    Join Date
    Dec 2007
    Posts
    11

    Re: CheckBox problem

    jshultz thnx for the help!It worked!
    But why didn't had to do the same thing for the other box (it was linked automatically)?

  6. #6
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: CheckBox problem

    Quote Originally Posted by skiabox
    jshultz thnx for the help!It worked!
    But why didn't had to do the same thing for the other box (it was linked automatically)?
    Did you double click on one of the checkboxes ? (i.e. healthyBox) Doing so will add an event handler automatically.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured