I would like to know if my C# program I have written conforms to the following r
ID: 3707830 • Letter: I
Question
I would like to know if my C# program I have written conforms to the following requirements;
"Now, you will add error handling into this project. Recall that you are using the int.TryParse() function to parse out the values entered by the user.
Using the Try/Catch statement, you want to handle any possible exceptions that may occur as a result of performing the operation on the two numbers. Use at least one specific Exception in your catch statement and handle any general exceptions."
\Program
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;
namespace Error_Handling
{
public partial class Form1 : Form
{
//Set up public variables
int num1, num2;
bool check1, check2;
String text1, text2;
public Form1()
{
InitializeComponent();
}
//Add Button
private void btnAdd_Click(object sender, EventArgs e)
{
//Set up variables and convert string to integer
text1 = txtNum1.Text;
check1 = int.TryParse(text1, out num1);
text2 = txtNum2.Text;
check2 = int.TryParse(text2, out num2);
//Check for valid inputs and dispaly message if error
if (check1 == false || check2 == false || (num1 < 0 || num2 < 0))
{
MessageBox.Show("Please enter a valid number");
}
else
//calculate
{
int res = num1 + num2;
txtResult.Text = Convert.ToString(res);
}
}
//Subtract Button
private void btnSubtract_Click(object sender, EventArgs e)
{
//Set up variables and convert string to integer
text1 = txtNum1.Text;
check1 = int.TryParse(text1, out num1);
text2 = txtNum2.Text;
check2 = int.TryParse(text2, out num2);
//Check for valid inputs and dispaly message if error
if (check1 == false || check2 == false || (num1 < 0 || num2 < 0))
{
MessageBox.Show("Please enter a valid number");
}
else
//calculate
{
int res = num1 - num2;
txtResult.Text = Convert.ToString(res);
}
}
//Multiply Button
private void btnMultiply_Click(object sender, EventArgs e)
{
//Set up variables and convert string to integer
text1 = txtNum1.Text;
check1 = int.TryParse(text1, out num1);
text2 = txtNum2.Text;
check2 = int.TryParse(text2, out num2);
//Check for valid inputs and dispaly message if error
if (check1 == false || check2 == false || (num1 < 0 || num2 < 0))
{
MessageBox.Show("Please enter a valid number");
}
else
//calculate
{
int res = num1 * num2;
txtResult.Text = Convert.ToString(res);
}
}
//Divide Button
private void btnDivde_Click(object sender, EventArgs e)
{
//Set up variables and convert string to integer
text1 = txtNum1.Text;
check1 = int.TryParse(text1, out num1);
text2 = txtNum2.Text;
check2 = int.TryParse(text2, out num2);
//Check for valid inputs and dispaly message if error
if (check1 == false || check2 == false || (num1 < 0 || num2 < 0))
{
MessageBox.Show("Please enter a valid number");
}
else
//calculate
try
{
int res = num1 / num2;
txtResult.Text = Convert.ToString(res);
}
//Divide by zero exception
catch(Exception divZeroEx)
{
MessageBox.Show("Cannot divide by zero.");
}
}
//Clear form
private void btnClear_Click(object sender, EventArgs e)
{
txtNum1.Text = "";
txtNum2.Text = "";
txtResult.Text = "";
}
//Close app
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Explanation / Answer
namespace SimpleCalculator
{
partial class Calculator_Form
{
/// <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.tb_num1 = new System.Windows.Forms.TextBox();
this.tb_num2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.rb_add = new System.Windows.Forms.RadioButton();
this.rb_sub = new System.Windows.Forms.RadioButton();
this.rb_mult = new System.Windows.Forms.RadioButton();
this.rb_divide = new System.Windows.Forms.RadioButton();
this.btn_result = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tb_num1
//
this.tb_num1.Location = new System.Drawing.Point(36, 67);
this.tb_num1.Name = "tb_num1";
this.tb_num1.Size = new System.Drawing.Size(119, 20);
this.tb_num1.TabIndex = 0;
//
// tb_num2
//
this.tb_num2.Location = new System.Drawing.Point(207, 67);
this.tb_num2.Name = "tb_num2";
this.tb_num2.Size = new System.Drawing.Size(123, 20);
this.tb_num2.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(38, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(87, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Enter 1st number";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(210, 36);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Enter 2nd number";
//
// rb_add
//
this.rb_add.AutoSize = true;
this.rb_add.Checked = true;
this.rb_add.Location = new System.Drawing.Point(36, 138);
this.rb_add.Name = "rb_add";
this.rb_add.Size = new System.Drawing.Size(44, 17);
this.rb_add.TabIndex = 4;
this.rb_add.TabStop = true;
this.rb_add.Text = "Add";
this.rb_add.UseVisualStyleBackColor = true;
//
// rb_sub
//
this.rb_sub.AutoSize = true;
this.rb_sub.Location = new System.Drawing.Point(104, 138);
this.rb_sub.Name = "rb_sub";
this.rb_sub.Size = new System.Drawing.Size(65, 17);
this.rb_sub.TabIndex = 5;
this.rb_sub.TabStop = true;
this.rb_sub.Text = "Subtract";
this.rb_sub.UseVisualStyleBackColor = true;
//
// rb_mult
//
this.rb_mult.AutoSize = true;
this.rb_mult.Location = new System.Drawing.Point(192, 138);
this.rb_mult.Name = "rb_mult";
this.rb_mult.Size = new System.Drawing.Size(60, 17);
this.rb_mult.TabIndex = 6;
this.rb_mult.TabStop = true;
this.rb_mult.Text = "Multiply";
this.rb_mult.UseVisualStyleBackColor = true;
//
// rb_divide
//
this.rb_divide.AutoSize = true;
this.rb_divide.Location = new System.Drawing.Point(271, 138);
this.rb_divide.Name = "rb_divide";
this.rb_divide.Size = new System.Drawing.Size(55, 17);
this.rb_divide.TabIndex = 7;
this.rb_divide.TabStop = true;
this.rb_divide.Text = "Divide";
this.rb_divide.UseVisualStyleBackColor = true;
//
// btn_result
//
this.btn_result.Location = new System.Drawing.Point(247, 197);
this.btn_result.Name = "btn_result";
this.btn_result.Size = new System.Drawing.Size(84, 23);
this.btn_result.TabIndex = 8;
this.btn_result.Text = "Show Result";
this.btn_result.UseVisualStyleBackColor = true;
this.btn_result.Click += new System.EventHandler(this.btn_result_Click);
//
// Calculator_Form
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(358, 263);
this.Controls.Add(this.btn_result);
this.Controls.Add(this.rb_divide);
this.Controls.Add(this.rb_mult);
this.Controls.Add(this.rb_sub);
this.Controls.Add(this.rb_add);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tb_num2);
this.Controls.Add(this.tb_num1);
this.Name = "Calculator_Form";
this.Text = "Simple Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox tb_num1;
private System.Windows.Forms.TextBox tb_num2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RadioButton rb_add;
private System.Windows.Forms.RadioButton rb_sub;
private System.Windows.Forms.RadioButton rb_mult;
private System.Windows.Forms.RadioButton rb_divide;
private System.Windows.Forms.Button btn_result;
}
}
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 SimpleCalculator
{
public partial class Calculator_Form : Form
{
public Calculator_Form()
{
MaximizeBox = false;
InitializeComponent();
}
/*
* Button Show result Click listener method
**/
private void btn_result_Click(object sender, EventArgs e)
{
int num1, num2;
bool tryConvert1 = int.TryParse(tb_num1.Text, out num1); // converting 1st textbox value to int
bool tryConvert2 = int.TryParse(tb_num2.Text, out num2); // converting 2nd textbox value to int
int result;
if (tryConvert1 == true && tryConvert2 == true) // if successfully both text box value converted then execute this block
{
if (rb_add.Checked == true)
{
result = num1 + num2;
MessageBox.Show("Sum of the input numbers is: " + result.ToString(), "Result");
}
else if (rb_sub.Checked == true)
{
result = num1 - num2;
MessageBox.Show("Difference of the input numbers is: " + result.ToString(), "Result");
}
else if (rb_mult.Checked == true)
{
result = num1 * num2;
MessageBox.Show("Product of the input numbers is: " + result.ToString(), "Result");
}
else if (rb_divide.Checked == true)
{
try
{
result = num1 / num2;
MessageBox.Show("Quotient is: " + result.ToString(), "Result");
}
catch (DivideByZeroException) // catch divide by 0 exception
{
MessageBox.Show("Error! can't divide by zero", "ERROR");
}
}
}
else
{
MessageBox.Show("Please input valid number(s) in both the text box(s)", "Message");
}
}
}
}
Direction: Open visual studio create a new C# windows form project with project name as SimpleCalculator and solution name as SimpleCalculatorSolution. After then change the Form1 name (not text) as Calculator_Form. Now copy the code of Form1.Designer.cs from first table above and paste (completely raplace) into the project's Form1.Designer.cs file and do the same with Form1.cs code file
namespace SimpleCalculator
{
partial class Calculator_Form
{
/// <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.tb_num1 = new System.Windows.Forms.TextBox();
this.tb_num2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.rb_add = new System.Windows.Forms.RadioButton();
this.rb_sub = new System.Windows.Forms.RadioButton();
this.rb_mult = new System.Windows.Forms.RadioButton();
this.rb_divide = new System.Windows.Forms.RadioButton();
this.btn_result = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// tb_num1
//
this.tb_num1.Location = new System.Drawing.Point(36, 67);
this.tb_num1.Name = "tb_num1";
this.tb_num1.Size = new System.Drawing.Size(119, 20);
this.tb_num1.TabIndex = 0;
//
// tb_num2
//
this.tb_num2.Location = new System.Drawing.Point(207, 67);
this.tb_num2.Name = "tb_num2";
this.tb_num2.Size = new System.Drawing.Size(123, 20);
this.tb_num2.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(38, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(87, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Enter 1st number";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(210, 36);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Enter 2nd number";
//
// rb_add
//
this.rb_add.AutoSize = true;
this.rb_add.Checked = true;
this.rb_add.Location = new System.Drawing.Point(36, 138);
this.rb_add.Name = "rb_add";
this.rb_add.Size = new System.Drawing.Size(44, 17);
this.rb_add.TabIndex = 4;
this.rb_add.TabStop = true;
this.rb_add.Text = "Add";
this.rb_add.UseVisualStyleBackColor = true;
//
// rb_sub
//
this.rb_sub.AutoSize = true;
this.rb_sub.Location = new System.Drawing.Point(104, 138);
this.rb_sub.Name = "rb_sub";
this.rb_sub.Size = new System.Drawing.Size(65, 17);
this.rb_sub.TabIndex = 5;
this.rb_sub.TabStop = true;
this.rb_sub.Text = "Subtract";
this.rb_sub.UseVisualStyleBackColor = true;
//
// rb_mult
//
this.rb_mult.AutoSize = true;
this.rb_mult.Location = new System.Drawing.Point(192, 138);
this.rb_mult.Name = "rb_mult";
this.rb_mult.Size = new System.Drawing.Size(60, 17);
this.rb_mult.TabIndex = 6;
this.rb_mult.TabStop = true;
this.rb_mult.Text = "Multiply";
this.rb_mult.UseVisualStyleBackColor = true;
//
// rb_divide
//
this.rb_divide.AutoSize = true;
this.rb_divide.Location = new System.Drawing.Point(271, 138);
this.rb_divide.Name = "rb_divide";
this.rb_divide.Size = new System.Drawing.Size(55, 17);
this.rb_divide.TabIndex = 7;
this.rb_divide.TabStop = true;
this.rb_divide.Text = "Divide";
this.rb_divide.UseVisualStyleBackColor = true;
//
// btn_result
//
this.btn_result.Location = new System.Drawing.Point(247, 197);
this.btn_result.Name = "btn_result";
this.btn_result.Size = new System.Drawing.Size(84, 23);
this.btn_result.TabIndex = 8;
this.btn_result.Text = "Show Result";
this.btn_result.UseVisualStyleBackColor = true;
this.btn_result.Click += new System.EventHandler(this.btn_result_Click);
//
// Calculator_Form
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(358, 263);
this.Controls.Add(this.btn_result);
this.Controls.Add(this.rb_divide);
this.Controls.Add(this.rb_mult);
this.Controls.Add(this.rb_sub);
this.Controls.Add(this.rb_add);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.tb_num2);
this.Controls.Add(this.tb_num1);
this.Name = "Calculator_Form";
this.Text = "Simple Calculator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox tb_num1;
private System.Windows.Forms.TextBox tb_num2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.RadioButton rb_add;
private System.Windows.Forms.RadioButton rb_sub;
private System.Windows.Forms.RadioButton rb_mult;
private System.Windows.Forms.RadioButton rb_divide;
private System.Windows.Forms.Button btn_result;
}
}