Link to home
Start Free TrialLog in
Avatar of Computer Guy
Computer Guy

asked on

C# Method Issue

Hi,

When I click a button, I need to take the user entered wholesale price and custom percentage and pass them to a method as arguments and then return the value to a label.

Where did I mess up?

      private double CalculateRetail(double wholesale, double percentage)
        {
            return wholesale * percentage;
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            double rp, retailPrice, wholesale, percentage;
            if (double.TryParse(wholesaleTextBox.Text, out wholesale))
            {
                if (double.TryParse(percentageTextBox.Text, out percentage))
                {
                    retailPrice = CalculateRetail(wholesale);

                    resultLabel.Text = rp.ToString("n1");
                }
                else
                {
                    MessageBox.Show("Enter a Valid Price");
                }
            }
        }


    }
}

    

Open in new window

Avatar of it_saige
it_saige
Flag of United States of America image

Try this (quick and dirty):

Form1.cs -
using System;
using System.Windows.Forms;

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

		private void OnClick(object sender, EventArgs e)
		{
			double wholesale, percentage;
			if (double.TryParse(tbPrice.Text, out wholesale))
			{
				if (double.TryParse(tbPercent.Text, out percentage))
					lblResults.Text = CalculateRetail(wholesale, percentage).ToString();
				else
					MessageBox.Show("Enter a valid percentage!");
			}
			else
				MessageBox.Show("Enter a valid price!");
		}

		private double CalculateRetail(double wholesale, double percentage)
		{
			return wholesale * percentage;
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28645837
{
	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.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.tbPrice = new System.Windows.Forms.TextBox();
			this.tbPercent = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.lblResults = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(197, 65);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "Calculate";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.OnClick);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(12, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(87, 13);
			this.label1.TabIndex = 1;
			this.label1.Text = "Wholesale Price:";
			// 
			// tbPrice
			// 
			this.tbPrice.Location = new System.Drawing.Point(105, 13);
			this.tbPrice.Name = "tbPrice";
			this.tbPrice.Size = new System.Drawing.Size(167, 20);
			this.tbPrice.TabIndex = 2;
			// 
			// tbPercent
			// 
			this.tbPercent.Location = new System.Drawing.Point(105, 39);
			this.tbPercent.Name = "tbPercent";
			this.tbPercent.Size = new System.Drawing.Size(167, 20);
			this.tbPercent.TabIndex = 4;
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(34, 42);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(65, 13);
			this.label2.TabIndex = 3;
			this.label2.Text = "Percentage:";
			// 
			// lblResults
			// 
			this.lblResults.AutoSize = true;
			this.lblResults.Location = new System.Drawing.Point(236, 95);
			this.lblResults.Name = "lblResults";
			this.lblResults.Size = new System.Drawing.Size(0, 13);
			this.lblResults.TabIndex = 5;
			// 
			// label4
			// 
			this.label4.AutoSize = true;
			this.label4.Location = new System.Drawing.Point(155, 95);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(45, 13);
			this.label4.TabIndex = 6;
			this.label4.Text = "Results:";
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 264);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.lblResults);
			this.Controls.Add(this.tbPercent);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.tbPrice);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox tbPrice;
		private System.Windows.Forms.TextBox tbPercent;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label lblResults;
		private System.Windows.Forms.Label label4;
	}
}

Open in new window

Produces the following results -User generated image-saige-
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial