In: Computer Science
Design an application that enables users to enter two values of type int. Write a method that retrieves the values and stores them and their products in separate structures. If you are designing a Windows application, you might allow the user to input the values in a TextBox and then retrieve and store the values in a ListBox. Their product could be stored in a separate ListBox. Your solution should include exception-handling techniques with a minimum of two catch clauses.
This is one programming assignment on Error Handling and debugging. Students do not need to submit a separate project for each part, just the finished project. Please remember to include you commented out each "exception" that caused an error (there should be 4).
This is for C#
As per your requirement I have written code which fulfill all your requirements please follow it step by step and i have included comments for better understanding
FormApp.Designer.cs
namespace ValuesProduct
{
partial class FormApp
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// </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.labelElement1 = new System.Windows.Forms.Label();
this.firstTextValue = new System.Windows.Forms.TextBox();
this.secondTextValue = new System.Windows.Forms.TextBox();
this.labelElement2 = new System.Windows.Forms.Label();
this.submitButton = new System.Windows.Forms.Button();
this.listTwoBoxValues = new System.Windows.Forms.ListBox();
this.listProductBox = new System.Windows.Forms.ListBox();
this.labelElement3 = new System.Windows.Forms.Label();
this.labelElement4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelElement1
//
this.labelElement1.AutoSize = true;
this.labelElement1.Location = new System.Drawing.Point(90,
46);
this.labelElement1.Name = "labelElement1";
this.labelElement1.Size = new System.Drawing.Size(84, 13);
this.labelElement1.TabIndex = 0;
this.labelElement1.Text = "Enter First Value";
//
// firstTextValue
//
this.firstTextValue.Location = new System.Drawing.Point(180,
43);
this.firstTextValue.Name = "firstTextValue";
this.firstTextValue.Size = new System.Drawing.Size(100, 20);
this.firstTextValue.TabIndex = 1;
this.firstTextValue.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.keyPressFirstTextValue);
//
// secondTextValue
//
this.secondTextValue.Location = new System.Drawing.Point(180,
80);
this.secondTextValue.Name = "secondTextValue";
this.secondTextValue.Size = new System.Drawing.Size(100, 20);
this.secondTextValue.TabIndex = 3;
this.secondTextValue.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.keyPressSecondTextValue);
//
// labelElement2
//
this.labelElement2.AutoSize = true;
this.labelElement2.Location = new System.Drawing.Point(72,
83);
this.labelElement2.Name = "labelElement2";
this.labelElement2.Size = new System.Drawing.Size(102, 13);
this.labelElement2.TabIndex = 2;
this.labelElement2.Text = "Enter Second Value";
//
// submitButton
//
this.submitButton.Location = new System.Drawing.Point(180,
107);
this.submitButton.Name = "submitButton";
this.submitButton.Size = new System.Drawing.Size(75, 23);
this.submitButton.TabIndex = 4;
this.submitButton.Text = "Submit";
this.submitButton.UseVisualStyleBackColor = true;
this.submitButton.Click += new
System.EventHandler(this.submitButtonClick);
//
// listTwoBoxValues
//
this.listTwoBoxValues.FormattingEnabled = true;
this.listTwoBoxValues.Location = new System.Drawing.Point(84,
165);
this.listTwoBoxValues.Name = "listTwoBoxValues";
this.listTwoBoxValues.Size = new System.Drawing.Size(99, 69);
this.listTwoBoxValues.TabIndex = 5;
//
// listProductBox
//
this.listProductBox.FormattingEnabled = true;
this.listProductBox.Location = new System.Drawing.Point(268,
165);
this.listProductBox.Name = "listProductBox";
this.listProductBox.Size = new System.Drawing.Size(99, 69);
this.listProductBox.TabIndex = 6;
//
// labelElement3
//
this.labelElement3.AutoSize = true;
this.labelElement3.Location = new System.Drawing.Point(81,
137);
this.labelElement3.Name = "labelElement3";
this.labelElement3.Size = new System.Drawing.Size(119, 13);
this.labelElement3.TabIndex = 7;
this.labelElement3.Text = "Store Enter Two Values";
//
// labelElement4
//
this.labelElement4.AutoSize = true;
this.labelElement4.Location = new System.Drawing.Point(262,
137);
this.labelElement4.Name = "labelElement4";
this.labelElement4.Size = new System.Drawing.Size(106, 13);
this.labelElement4.TabIndex = 8;
this.labelElement4.Text = "Tow Values Product ";
//
// FormApp
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(424, 299);
this.Controls.Add(this.labelElement4);
this.Controls.Add(this.labelElement3);
this.Controls.Add(this.listProductBox);
this.Controls.Add(this.listTwoBoxValues);
this.Controls.Add(this.submitButton);
this.Controls.Add(this.secondTextValue);
this.Controls.Add(this.labelElement2);
this.Controls.Add(this.firstTextValue);
this.Controls.Add(this.labelElement1);
this.Name = "FormApp";
this.Text = "Product Two Values";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label labelElement1;
private System.Windows.Forms.TextBox firstTextValue;
private System.Windows.Forms.TextBox secondTextValue;
private System.Windows.Forms.Label labelElement2;
private System.Windows.Forms.Button submitButton;
private System.Windows.Forms.ListBox listTwoBoxValues;
private System.Windows.Forms.ListBox listProductBox;
private System.Windows.Forms.Label labelElement3;
private System.Windows.Forms.Label labelElement4;
}
}
FormApp.cs
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 ValuesProduct
{
public partial class FormApp : Form
{
public FormApp()
{
InitializeComponent();
}
private void submitButtonClick(object sender, EventArgs e)
{
try
{
if (firstTextValue.Text =="")
{
MessageBox.Show("Enter First Value");
firstTextValue.Focus();
return;
}
if (secondTextValue.Text =="")
{
MessageBox.Show("Enter Second Value");
secondTextValue.Focus();
return;
}
int valueFirstContent = Convert.ToInt32(firstTextValue.Text);
int valueSecondContent =
Convert.ToInt32(secondTextValue.Text);
int product=valueFirstContent*valueSecondContent;
AddValuesListBox(valueFirstContent, valueSecondContent);
ProductValuesListBox(product);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.InnerException.ToString());
}
}
private void AddValuesListBox(int valueFirstContent, int
valueSecondContent)
{
try
{
listTwoBoxValues.Items.Add(valueFirstContent);
listTwoBoxValues.Items.Add(valueSecondContent);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.InnerException.ToString());
}
}
private void ProductValuesListBox(int product)
{
try
{
listProductBox.Items.Add(product);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.InnerException.ToString());
}
}
private void keyPressFirstTextValue(object sender,
KeyPressEventArgs e)
{
// only allow numbers
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)
)
{
e.Handled = true;
}
}
private void keyPressSecondTextValue(object sender,
KeyPressEventArgs e)
{
// only allow numbers
if (!char.IsControl(e.KeyChar) &&
!char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
}