C# Program. Create a Windows application that has the functionality of a calculator and works
with integral values. Allow the user to select buttons representing numeric values. If the user
attempts to divide by zero, throw and handle an exception.
Solution
below is the code from the windows application in visual studio:
Form1.cs
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 frmCalculator : Form
{
string operand1 = string.Empty;
string operand2 = string.Empty;
string result;
char operation;
public frmCalculator()
{
InitializeComponent();
}
private void frmCalculator_Load(object sender, EventArgs e)
{
btnOne.Click += new EventHandler(btn_Click);
btnTwo.Click += new EventHandler(btn_Click);
btnThree.Click += new EventHandler(btn_Click);
btnFour.Click += new EventHandler(btn_Click);
btnFive.Click += new EventHandler(btn_Click);
btnSix.Click += new EventHandler(btn_Click);
btnSeven.Click += new EventHandler(btn_Click);
btnEight.Click += new EventHandler(btn_Click);
btnNine.Click += new EventHandler(btn_Click);
btnZero.Click += new EventHandler(btn_Click);
btnDot.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
try
{
Button btn = sender as Button;
switch (btn.Name)
{
case \"btnOne\":
txtInput.Text += \"1\";
break;
case \"btnTwo\":
txtInput.Text += \"2\";
break;
case \"btnThree\":
txtInput.Text += \"3\";
break;
case \"btnFour\":
txtInput.Text += \"4\";
break;
case \"btnFive\":
txtInput.Text += \"5\";
break;
case \"btnSix\":
txtInput.Text += \"6\";
break;
case \"btnSeven\":
txtInput.Text += \"7\";
break;
case \"btnEight\":
txtInput.Text += \"8\";
break;
case \"btnNine\":
txtInput.Text += \"9\";
break;
case \"btnZero\":
txtInput.Text += \"0\";
break;
case \"btnDot\":
if(!txtInput.Text.Contains(\".\"))
txtInput.Text += \".\";
break;
}
}
catch(Exception ex)
{
MessageBox.Show(\"Sorry for the inconvenience, Unexpected error occured. Details:
\" +
ex.Message);
}
}
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case \'1\':
case \'2\':
case \'3\':
case \'4\':
case \'5\':
case \'6\':
case \'7\':
case \'8\':
case \'9\':
case \'0\':
//case \'+\':
//case \'-\':
//case \'*\':
//case \'/\':
//case \'.\':
break;
default:
e.Handled = true;
MessageBox.Show(\"Only numbers, +, -, ., *, / are allowed\");
break;
}
}
private void txtInput_TextChanged(object sender, EventArgs e)
{
}
private void btnPlus_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = \'+\';
txtInput.Text = string.Empty;
}
private void btnMinus_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = \'-\';
txtInput.Text = string.Empty;
}
private void btnMulitply_Click(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = \'*\';
txtInput.Text = string.Empty;
}
private void btnDivide_Click.
Role Of Transgenic Animal In Target Validation-1.pptx
C# Program. Create a Windows application that has the functionality .pdf
1. C# Program. Create a Windows application that has the functionality of a calculator and works
with integral values. Allow the user to select buttons representing numeric values. If the user
attempts to divide by zero, throw and handle an exception.
Solution
below is the code from the windows application in visual studio:
Form1.cs
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 frmCalculator : Form
{
string operand1 = string.Empty;
string operand2 = string.Empty;
string result;
char operation;
public frmCalculator()
{
InitializeComponent();
}
private void frmCalculator_Load(object sender, EventArgs e)
{
btnOne.Click += new EventHandler(btn_Click);
btnTwo.Click += new EventHandler(btn_Click);
btnThree.Click += new EventHandler(btn_Click);
btnFour.Click += new EventHandler(btn_Click);
btnFive.Click += new EventHandler(btn_Click);
2. btnSix.Click += new EventHandler(btn_Click);
btnSeven.Click += new EventHandler(btn_Click);
btnEight.Click += new EventHandler(btn_Click);
btnNine.Click += new EventHandler(btn_Click);
btnZero.Click += new EventHandler(btn_Click);
btnDot.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
try
{
Button btn = sender as Button;
switch (btn.Name)
{
case "btnOne":
txtInput.Text += "1";
break;
case "btnTwo":
txtInput.Text += "2";
break;
case "btnThree":
txtInput.Text += "3";
break;
case "btnFour":
txtInput.Text += "4";
break;
case "btnFive":
txtInput.Text += "5";
break;
case "btnSix":
txtInput.Text += "6";
break;
case "btnSeven":
txtInput.Text += "7";
break;
case "btnEight":
3. txtInput.Text += "8";
break;
case "btnNine":
txtInput.Text += "9";
break;
case "btnZero":
txtInput.Text += "0";
break;
case "btnDot":
if(!txtInput.Text.Contains("."))
txtInput.Text += ".";
break;
}
}
catch(Exception ex)
{
MessageBox.Show("Sorry for the inconvenience, Unexpected error occured. Details:
" +
ex.Message);
}
}
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
//case '+':
7. /// 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.txtInput = new System.Windows.Forms.TextBox();
this.btnOne = new System.Windows.Forms.Button();
this.btnTwo = new System.Windows.Forms.Button();
this.btnThree = new System.Windows.Forms.Button();
this.btnFour = new System.Windows.Forms.Button();
this.btnFive = new System.Windows.Forms.Button();
this.btnSix = new System.Windows.Forms.Button();
this.btnSeven = new System.Windows.Forms.Button();
this.btnEight = new System.Windows.Forms.Button();
this.btnNine = new System.Windows.Forms.Button();
this.btnZero = new System.Windows.Forms.Button();
this.btnDot = new System.Windows.Forms.Button();
this.btnEqual = new System.Windows.Forms.Button();
this.btnPlus = new System.Windows.Forms.Button();
8. this.btnMinus = new System.Windows.Forms.Button();
this.btnMulitply = new System.Windows.Forms.Button();
this.btnDivide = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnSqrRoot = new System.Windows.Forms.Button();
this.btnByTwo = new System.Windows.Forms.Button();
this.btnByFour = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(13, 13);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(193, 20);
this.txtInput.TabIndex = 0;
this.txtInput.TextChanged += new System.EventHandler(this.txtInput_TextChanged);
this.txtInput.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
//
// btnOne
//
this.btnOne.Location = new System.Drawing.Point(12, 39);
this.btnOne.Name = "btnOne";
this.btnOne.Size = new System.Drawing.Size(34, 23);
this.btnOne.TabIndex = 1;
this.btnOne.Text = "1";
this.btnOne.UseVisualStyleBackColor = true;
//
// btnTwo
//
this.btnTwo.Location = new System.Drawing.Point(52, 39);
this.btnTwo.Name = "btnTwo";
this.btnTwo.Size = new System.Drawing.Size(34, 23);
this.btnTwo.TabIndex = 2;
this.btnTwo.Text = "2";
this.btnTwo.UseVisualStyleBackColor = true;