Create a windows application that contains two textboxes and three buttons. The textboxes should be used to allow the user to input two positive numeric values. The buttons should be labeled Add, Multiply and Reset. Create event handler methods that retrieve the values, perform the calculations and display the result of the calculations on a label. The result label should initially be set to invisible with a font color of yellow. If invalid data is entered, change the font color to red on the result label and display the message saying “value must be numeri and >0”. When the final answer is displayed, the font color should be yellow. Additional labels will be needed from the textboxes captions. Do not allow non numeric characters to be entered. Invoke the TryParse() method to retrieve the values. All controls involved in program statements should be named. Right justify values in the textbox.
namespace Calculations
{
partial class frmMain
{
/// <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.btnAdd = new System.Windows.Forms.Button();
this.btnMultiply = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtNumber1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtNumber2 = new System.Windows.Forms.TextBox();
this.lblResult = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(237, 12);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 0;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnMultiply
//
this.btnMultiply.Location = new System.Drawing.Point(237, 41);
this.btnMultiply.Name = "btnMultiply";
this.btnMultiply.Size = new System.Drawing.Size(75, 23);
this.btnMultiply.TabIndex = 0;
this.btnMultiply.Text = "Multiply";
this.btnMultiply.UseVisualStyleBackColor = true;
this.btnMultiply.Click += new System.EventHandler(this.btnMultiply_Click);
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(237, 70);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(75, 23);
this.btnReset.TabIndex = 0;
this.btnReset.Text = "Reset";
this.btnReset.UseVisualStyleBackColor = true;
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(19, 17);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(82, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Enter number 1:";
//
// txtNumber1
//
this.txtNumber1.Location = new System.Drawing.Point(107, 14);
this.txtNumber1.Name = "txtNumber1";
this.txtNumber1.Size = new System.Drawing.Size(100, 20);
this.txtNumber1.TabIndex = 2;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(19, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(82, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Enter number 2:";
//
// txtNumber2
//
this.txtNumber2.Location = new System.Drawing.Point(107, 40);
this.txtNumber2.Name = "txtNumber2";
this.txtNumber2.Size = new System.Drawing.Size(100, 20);
this.txtNumber2.TabIndex = 2;
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(19, 75);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(49, 13);
this.lblResult.TabIndex = 1;
this.lblResult.Text = "Result: 0";
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(332, 120);
this.Controls.Add(this.txtNumber2);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtNumber1);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnMultiply);
this.Controls.Add(this.btnAdd);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Calculations";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnMultiply;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtNumber1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtNumber2;
private System.Windows.Forms.Label lblResult;
}
}
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 Calculations
{
public partial class frmMain : Form
{
private double number1;
private double number2;
private double result;
public frmMain()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (isCorrectNumbers()) {
//When the final answer is displayed, the font color should be yellow.
this.BackColor=Color.Yellow;
result = number1 + number2;
lblResult.Text = "Result: " + result.ToString();
}
}
private bool isCorrectNumbers() {
//If invalid data is entered, change the font color to red on the result label and
//display the message saying "value must be numeri and >0".
if (!double.TryParse(txtNumber1.Text, out number1) || number1 < 0)
{
MessageBox.Show("Value must be numeric and >0");
txtNumber1.Text = "";
txtNumber1.Focus();
lblResult.ForeColor = Color.Red;
return false;
}
if (!double.TryParse(txtNumber2.Text, out number2) || number2 < 0)
{
MessageBox.Show("Value must be numeric and >0");
txtNumber2.Text = "";
txtNumber2.Focus();
lblResult.ForeColor = Color.Red;
return false;
}
lblResult.ForeColor = Color.Black;
return true;
}
private void btnMultiply_Click(object sender, EventArgs e)
{
if (isCorrectNumbers())
{
//When the final answer is displayed, the font color should be yellow.
this.BackColor = Color.Yellow;
result = number1 *number2;
lblResult.Text = "Result: " + result.ToString();
}
}
private void btnReset_Click(object sender, EventArgs e)
{
txtNumber1.Text = "";
txtNumber2.Text = "";
lblResult.Text = "Result: 0";
this.BackColor = SystemColors.Control;
}
}
}
Comments
Leave a comment