In: Computer Science
Assign 2: Add to the “Area” project from Lab #1.
Add a “Clear” button to this project. Also make the Clear button work, clear should clear all textbaoxes
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new Windows Forms Application in C# is created using Visual Studio 2017 with name "Area".This application contains a form with name "Form1.cs".Below are the files associated with form1.
1.Form1.cs[Design]
2.Form1.Designer.cs
namespace Area
{
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.lblRadius = new System.Windows.Forms.Label();
this.btnCalcArea = new System.Windows.Forms.Button();
this.rbtCircle = new System.Windows.Forms.RadioButton();
this.btnClear = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.txtRadius = new System.Windows.Forms.TextBox();
this.txtWidth = new System.Windows.Forms.TextBox();
this.lblWidth = new System.Windows.Forms.Label();
this.txtArea = new System.Windows.Forms.TextBox();
this.lblArea = new System.Windows.Forms.Label();
this.rbtSquare = new System.Windows.Forms.RadioButton();
this.rbtRectangle = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// lblRadius
//
this.lblRadius.AutoSize = true;
this.lblRadius.Location = new System.Drawing.Point(12, 18);
this.lblRadius.Name = "lblRadius";
this.lblRadius.Size = new System.Drawing.Size(40, 13);
this.lblRadius.TabIndex = 0;
this.lblRadius.Text = "Radius";
//
// btnCalcArea
//
this.btnCalcArea.Location = new System.Drawing.Point(27,
120);
this.btnCalcArea.Name = "btnCalcArea";
this.btnCalcArea.Size = new System.Drawing.Size(75, 23);
this.btnCalcArea.TabIndex = 1;
this.btnCalcArea.Text = "&Calc Area";
this.btnCalcArea.UseVisualStyleBackColor = true;
this.btnCalcArea.Click += new
System.EventHandler(this.btnCalcArea_Click);
//
// rbtCircle
//
this.rbtCircle.AutoSize = true;
this.rbtCircle.Location = new System.Drawing.Point(218, 18);
this.rbtCircle.Name = "rbtCircle";
this.rbtCircle.Size = new System.Drawing.Size(51, 17);
this.rbtCircle.TabIndex = 2;
this.rbtCircle.TabStop = true;
this.rbtCircle.Text = "Circle";
this.rbtCircle.UseVisualStyleBackColor = true;
this.rbtCircle.CheckedChanged += new
System.EventHandler(this.rbtCircle_CheckedChanged);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(120, 120);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 3;
this.btnClear.Text = "C&lear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new
System.EventHandler(this.btnClear_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(217, 120);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 4;
this.btnExit.Text = "E&xit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new
System.EventHandler(this.btnExit_Click);
//
// txtRadius
//
this.txtRadius.Location = new System.Drawing.Point(66, 15);
this.txtRadius.Name = "txtRadius";
this.txtRadius.Size = new System.Drawing.Size(100, 20);
this.txtRadius.TabIndex = 5;
//
// txtWidth
//
this.txtWidth.Location = new System.Drawing.Point(66, 47);
this.txtWidth.Name = "txtWidth";
this.txtWidth.Size = new System.Drawing.Size(100, 20);
this.txtWidth.TabIndex = 7;
//
// lblWidth
//
this.lblWidth.AutoSize = true;
this.lblWidth.Location = new System.Drawing.Point(12, 50);
this.lblWidth.Name = "lblWidth";
this.lblWidth.Size = new System.Drawing.Size(35, 13);
this.lblWidth.TabIndex = 6;
this.lblWidth.Text = "Width";
//
// txtArea
//
this.txtArea.Location = new System.Drawing.Point(66, 77);
this.txtArea.Name = "txtArea";
this.txtArea.Size = new System.Drawing.Size(100, 20);
this.txtArea.TabIndex = 9;
//
// lblArea
//
this.lblArea.AutoSize = true;
this.lblArea.Location = new System.Drawing.Point(12, 80);
this.lblArea.Name = "lblArea";
this.lblArea.Size = new System.Drawing.Size(29, 13);
this.lblArea.TabIndex = 8;
this.lblArea.Text = "Area";
//
// rbtSquare
//
this.rbtSquare.AutoSize = true;
this.rbtSquare.Location = new System.Drawing.Point(218, 50);
this.rbtSquare.Name = "rbtSquare";
this.rbtSquare.Size = new System.Drawing.Size(59, 17);
this.rbtSquare.TabIndex = 10;
this.rbtSquare.TabStop = true;
this.rbtSquare.Text = "Square";
this.rbtSquare.UseVisualStyleBackColor = true;
this.rbtSquare.CheckedChanged += new
System.EventHandler(this.rbtSquare_CheckedChanged);
//
// rbtRectangle
//
this.rbtRectangle.AutoSize = true;
this.rbtRectangle.Location = new System.Drawing.Point(218,
80);
this.rbtRectangle.Name = "rbtRectangle";
this.rbtRectangle.Size = new System.Drawing.Size(74, 17);
this.rbtRectangle.TabIndex = 11;
this.rbtRectangle.TabStop = true;
this.rbtRectangle.Text = "Rectangle";
this.rbtRectangle.UseVisualStyleBackColor = true;
this.rbtRectangle.CheckedChanged += new
System.EventHandler(this.rbtRectangle_CheckedChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(312, 171);
this.Controls.Add(this.rbtRectangle);
this.Controls.Add(this.rbtSquare);
this.Controls.Add(this.txtArea);
this.Controls.Add(this.lblArea);
this.Controls.Add(this.txtWidth);
this.Controls.Add(this.lblWidth);
this.Controls.Add(this.txtRadius);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.rbtCircle);
this.Controls.Add(this.btnCalcArea);
this.Controls.Add(this.lblRadius);
this.Name = "Form1";
this.Text = "Area";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblRadius;
private System.Windows.Forms.Button btnCalcArea;
private System.Windows.Forms.RadioButton rbtCircle;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.TextBox txtRadius;
private System.Windows.Forms.TextBox txtWidth;
private System.Windows.Forms.Label lblWidth;
private System.Windows.Forms.TextBox txtArea;
private System.Windows.Forms.Label lblArea;
private System.Windows.Forms.RadioButton rbtSquare;
private System.Windows.Forms.RadioButton rbtRectangle;
}
}
3.Form1.cs
//namespace
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;
//application namespace
namespace Area
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Form load event
private void Form1_Load(object sender, EventArgs e)
{
//hide all labels and textboxes
lblRadius.Visible = false;
lblWidth.Visible = false;
lblArea.Visible = false;
txtArea.Visible = false;
txtRadius.Visible = false;
txtWidth.Visible = false;
}
//when circle radio button is selected
private void rbtCircle_CheckedChanged(object sender, EventArgs
e)
{
//make label and textbox visible
lblRadius.Visible = true;
txtRadius.Visible = true;
txtWidth.Visible = false;
lblWidth.Visible = false;
lblArea.Visible = false;
txtArea.Visible = false;
}
//when square radio button is selected
private void rbtSquare_CheckedChanged(object sender, EventArgs
e)
{
//make label and textbox visible
lblRadius.Visible = true;
lblRadius.Text = "Side";
txtRadius.Visible = true;
txtWidth.Visible = false;
lblWidth.Visible = false;
}
//when rectangle radio button is selected
private void rbtRectangle_CheckedChanged(object sender, EventArgs
e)
{
//make label and textbox visible
lblRadius.Visible = true;
lblRadius.Text = "Height";
txtRadius.Visible = true;
txtWidth.Visible = true;
lblWidth.Visible = true;
lblArea.Visible = false;
txtArea.Visible = false;
}
//calculate button click
private void btnCalcArea_Click(object sender, EventArgs e)
{
//checking which radio button is selected
if(rbtCircle.Checked==true)
{
//if circle radio button is selected then
//taking radius enered by user
double radius = double.Parse(txtRadius.Text);
//calculate and display area of circle
txtArea.Text = (Math.PI * radius * radius).ToString("0.00");
txtArea.Visible = true;
lblArea.Visible = true;
}
//checking which radio is selected
else if (rbtSquare.Checked == true)
{
//if Square radio button is selected then
//taking side enered by user
double side = double.Parse(txtRadius.Text);
//calculate and display area of square
txtArea.Text = (side*side).ToString("0.00");
txtArea.Visible = true;
lblArea.Visible = true;
}
//checking which radio is selected
else if (rbtRectangle.Checked == true)
{
//if rectangle radio button is selected then
//taking height and width enered by user
double height = double.Parse(txtRadius.Text);
//taking width entered by user
double width = double.Parse(txtWidth.Text);
//calculate and display area of rectangle
txtArea.Text = (height * width).ToString("0.00");
txtArea.Visible = true;
lblArea.Visible = true;
}
}
//Clear button click
private void btnClear_Click(object sender, EventArgs e)
{
//when clear button clicked ,clear all textboxes
txtArea.Text = "";
txtRadius.Text = "";
txtWidth.Text = "";
}
//Exit button click
private void btnExit_Click(object sender, EventArgs e)
{
//When exit button clicked , close the form
this.Close();
}
}
}
======================================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :
Screen 2 :Screen showing area of circle
Screen 3 :Screen showing area of square
Screen 4 :Screen showing area of rectangle
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.