Question: In a previous project, I was asked to create a simple Windows application that allowed a user to input two values and then add, subtract,

In a previous project, I was asked to create a simple Windows application that allowed a user to input two values and then add, subtract, multiply, or divide them. Now, I have been asked to add error handling into this project.

Here are the instructions I was asked to follow:

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.

I understand how to use the int.TryParse () function but I need help with the rest such as using the Try/Catch statement to handle any possible exceptions.

Here's what I have so far:

Form1.cs

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

private void button1_Click(object sender, EventArgs e) { int firstNo, secondNo; bool first, second; first = int.TryParse(textBox1.Text, out firstNo); second = int.TryParse(textBox2.Text, out secondNo); if (first && second) result.Text = "Solution: " + firstNo + " + " + secondNo + " = " + (firstNo + secondNo); else MessageBox.Show("Please enter a valid integer number");

}

private void button2_Click(object sender, EventArgs e) { int firstNo, secondNo; bool first, second; first = int.TryParse(textBox1.Text, out firstNo); second = int.TryParse(textBox2.Text, out secondNo); if (first && second) result.Text = "Solution: " + firstNo + " - " + secondNo + " = " + (firstNo - secondNo); else MessageBox.Show("Please enter a valid integer number"); }

private void button3_Click(object sender, EventArgs e) { int firstNo, secondNo; bool first, second; first = int.TryParse(textBox1.Text, out firstNo); second = int.TryParse(textBox2.Text, out secondNo); if (first && second) result.Text = "Solution: " + firstNo + " * " + secondNo + " = " + (firstNo * secondNo); else MessageBox.Show("Please enter a valid integer number"); }

private void button4_Click(object sender, EventArgs e) { int firstNo, secondNo; bool first, second; first = int.TryParse(textBox1.Text, out firstNo); second = int.TryParse(textBox2.Text, out secondNo); if (first && second) result.Text = "Solution: " + firstNo + " / " + secondNo + " = " + (firstNo / secondNo); else MessageBox.Show("Please enter a valid integer number"); } } }

Form1.Designer.cs

{ partial class Form1 { ///

/// Required designer variable. /// private System.ComponentModel.IContainer components = null;

///

/// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.result = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.SuspendLayout(); this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(152, 57); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(72, 13); this.label1.TabIndex = 0; this.label1.Text = "First Number :"; this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(152, 98); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(90, 13); this.label2.TabIndex = 1; this.label2.Text = "Second Number :"; this.textBox1.Location = new System.Drawing.Point(255, 57); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 2; this.textBox2.Location = new System.Drawing.Point(255, 98); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 3; this.result.AutoSize = true; this.result.Location = new System.Drawing.Point(222, 220); this.result.Name = "result"; this.result.Size = new System.Drawing.Size(0, 13); this.result.TabIndex = 4; this.button1.Location = new System.Drawing.Point(100, 150); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 50); this.button1.TabIndex = 5; this.button1.Text = "Add"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Location = new System.Drawing.Point(200, 150); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 50); this.button2.TabIndex = 6; this.button2.Text = "Subtract"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); this.button3.Location = new System.Drawing.Point(296, 150); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 50); this.button3.TabIndex = 7; this.button3.Text = "Multiply"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); this.button4.Location = new System.Drawing.Point(395, 150); this.button4.Name = "button4"; this.button4.Size = new System.Drawing.Size(75, 50); this.button4.TabIndex = 8; this.button4.Text = "Divide"; this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.button4_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.ControlLightLight; this.ClientSize = new System.Drawing.Size(575, 261); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.result); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.ForeColor = System.Drawing.SystemColors.WindowText; this.Name = "Form1"; this.Text = "Simple Calculator"; this.ResumeLayout(false); this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label result; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!