SlideShare a Scribd company logo
1 of 16
Download to read offline
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(object sender, EventArgs e)
{
operand1 = txtInput.Text;
operation = '/';
txtInput.Text = string.Empty;
}
private void btnEqual_Click(object sender, EventArgs e)
{
operand2 = txtInput.Text;
double opr1, opr2;
double.TryParse(operand1, out opr1);
double.TryParse(operand2, out opr2);
switch (operation)
{
case '+':
result = (opr1 + opr2).ToString();
break;
case '-':
result = (opr1 - opr2).ToString();
break;
case '*':
result = (opr1 * opr2).ToString();
break;
case '/':
if (opr2 != 0)
{
result = (opr1 / opr2).ToString();
}
else
{
MessageBox.Show("Can't divide by zero");
}
break;
}
txtInput.Text = result.ToString();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
}
private void btnSqrRoot_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (Math.Sqrt(opr1)).ToString();
}
}
private void btnByTwo_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (opr1 / 2).ToString();
}
}
private void btnByFour_Click(object sender, EventArgs e)
{
double opr1;
if (double.TryParse(txtInput.Text, out opr1))
{
txtInput.Text = (opr1 / 4).ToString();
}
}
}
}
Form1.Designer.cs
namespace SimpleCalculator
{
partial class frmCalculator
{
///
/// 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();
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;
//
// btnThree
//
this.btnThree.Location = new System.Drawing.Point(92, 39);
this.btnThree.Name = "btnThree";
this.btnThree.Size = new System.Drawing.Size(34, 23);
this.btnThree.TabIndex = 3;
this.btnThree.Text = "3";
this.btnThree.UseVisualStyleBackColor = true;
//
// btnFour
//
this.btnFour.Location = new System.Drawing.Point(12, 68);
this.btnFour.Name = "btnFour";
this.btnFour.Size = new System.Drawing.Size(34, 23);
this.btnFour.TabIndex = 4;
this.btnFour.Text = "4";
this.btnFour.UseVisualStyleBackColor = true;
//
// btnFive
//
this.btnFive.Location = new System.Drawing.Point(52, 68);
this.btnFive.Name = "btnFive";
this.btnFive.Size = new System.Drawing.Size(34, 23);
this.btnFive.TabIndex = 5;
this.btnFive.Text = "5";
this.btnFive.UseVisualStyleBackColor = true;
//
// btnSix
//
this.btnSix.Location = new System.Drawing.Point(92, 68);
this.btnSix.Name = "btnSix";
this.btnSix.Size = new System.Drawing.Size(34, 23);
this.btnSix.TabIndex = 6;
this.btnSix.Text = "6";
this.btnSix.UseVisualStyleBackColor = true;
//
// btnSeven
//
this.btnSeven.Location = new System.Drawing.Point(13, 97);
this.btnSeven.Name = "btnSeven";
this.btnSeven.Size = new System.Drawing.Size(34, 23);
this.btnSeven.TabIndex = 7;
this.btnSeven.Text = "7";
this.btnSeven.UseVisualStyleBackColor = true;
//
// btnEight
//
this.btnEight.Location = new System.Drawing.Point(53, 97);
this.btnEight.Name = "btnEight";
this.btnEight.Size = new System.Drawing.Size(34, 23);
this.btnEight.TabIndex = 8;
this.btnEight.Text = "8";
this.btnEight.UseVisualStyleBackColor = true;
//
// btnNine
//
this.btnNine.Location = new System.Drawing.Point(93, 97);
this.btnNine.Name = "btnNine";
this.btnNine.Size = new System.Drawing.Size(34, 23);
this.btnNine.TabIndex = 9;
this.btnNine.Text = "9";
this.btnNine.UseVisualStyleBackColor = true;
//
// btnZero
//
this.btnZero.Location = new System.Drawing.Point(13, 126);
this.btnZero.Name = "btnZero";
this.btnZero.Size = new System.Drawing.Size(34, 23);
this.btnZero.TabIndex = 10;
this.btnZero.Text = "0";
this.btnZero.UseVisualStyleBackColor = true;
//
// btnDot
//
this.btnDot.Location = new System.Drawing.Point(52, 126);
this.btnDot.Name = "btnDot";
this.btnDot.Size = new System.Drawing.Size(34, 23);
this.btnDot.TabIndex = 11;
this.btnDot.Text = ".";
this.btnDot.UseVisualStyleBackColor = true;
//
// btnEqual
//
this.btnEqual.Location = new System.Drawing.Point(93, 126);
this.btnEqual.Name = "btnEqual";
this.btnEqual.Size = new System.Drawing.Size(34, 23);
this.btnEqual.TabIndex = 12;
this.btnEqual.Text = "=";
this.btnEqual.UseVisualStyleBackColor = true;
this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click);
//
// btnPlus
//
this.btnPlus.Location = new System.Drawing.Point(132, 39);
this.btnPlus.Name = "btnPlus";
this.btnPlus.Size = new System.Drawing.Size(34, 23);
this.btnPlus.TabIndex = 13;
this.btnPlus.Text = "+";
this.btnPlus.UseVisualStyleBackColor = true;
this.btnPlus.Click += new System.EventHandler(this.btnPlus_Click);
//
// btnMinus
//
this.btnMinus.Location = new System.Drawing.Point(132, 68);
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(34, 23);
this.btnMinus.TabIndex = 14;
this.btnMinus.Text = "-";
this.btnMinus.UseVisualStyleBackColor = true;
this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click);
//
// btnMulitply
//
this.btnMulitply.Location = new System.Drawing.Point(132, 97);
this.btnMulitply.Name = "btnMulitply";
this.btnMulitply.Size = new System.Drawing.Size(34, 23);
this.btnMulitply.TabIndex = 15;
this.btnMulitply.Text = "*";
this.btnMulitply.UseVisualStyleBackColor = true;
this.btnMulitply.Click += new System.EventHandler(this.btnMulitply_Click);
//
// btnDivide
//
this.btnDivide.Location = new System.Drawing.Point(132, 126);
this.btnDivide.Name = "btnDivide";
this.btnDivide.Size = new System.Drawing.Size(34, 23);
this.btnDivide.TabIndex = 16;
this.btnDivide.Text = "/";
this.btnDivide.UseVisualStyleBackColor = true;
this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(172, 39);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(34, 23);
this.btnClear.TabIndex = 17;
this.btnClear.Text = "C";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnSqrRoot
//
this.btnSqrRoot.Location = new System.Drawing.Point(172, 68);
this.btnSqrRoot.Name = "btnSqrRoot";
this.btnSqrRoot.Size = new System.Drawing.Size(34, 23);
this.btnSqrRoot.TabIndex = 18;
this.btnSqrRoot.Text = "";
this.btnSqrRoot.UseVisualStyleBackColor = true;
this.btnSqrRoot.Click += new System.EventHandler(this.btnSqrRoot_Click);
//
// btnByTwo
//
this.btnByTwo.Location = new System.Drawing.Point(172, 97);
this.btnByTwo.Name = "btnByTwo";
this.btnByTwo.Size = new System.Drawing.Size(34, 23);
this.btnByTwo.TabIndex = 19;
this.btnByTwo.Text = "½";
this.btnByTwo.UseVisualStyleBackColor = true;
this.btnByTwo.Click += new System.EventHandler(this.btnByTwo_Click);
//
// btnByFour
//
this.btnByFour.Location = new System.Drawing.Point(172, 126);
this.btnByFour.Name = "btnByFour";
this.btnByFour.Size = new System.Drawing.Size(34, 23);
this.btnByFour.TabIndex = 20;
this.btnByFour.Text = "¼";
this.btnByFour.UseVisualStyleBackColor = true;
this.btnByFour.Click += new System.EventHandler(this.btnByFour_Click);
//
// frmCalculator
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(214, 157);
this.Controls.Add(this.btnByFour);
this.Controls.Add(this.btnByTwo);
this.Controls.Add(this.btnSqrRoot);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnDivide);
this.Controls.Add(this.btnMulitply);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnPlus);
this.Controls.Add(this.btnEqual);
this.Controls.Add(this.btnDot);
this.Controls.Add(this.btnZero);
this.Controls.Add(this.btnNine);
this.Controls.Add(this.btnEight);
this.Controls.Add(this.btnSeven);
this.Controls.Add(this.btnSix);
this.Controls.Add(this.btnFive);
this.Controls.Add(this.btnFour);
this.Controls.Add(this.btnThree);
this.Controls.Add(this.btnTwo);
this.Controls.Add(this.btnOne);
this.Controls.Add(this.txtInput);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmCalculator";
this.Text = "Simple Calculator";
this.Load += new System.EventHandler(this.frmCalculator_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Button btnOne;
private System.Windows.Forms.Button btnTwo;
private System.Windows.Forms.Button btnThree;
private System.Windows.Forms.Button btnFour;
private System.Windows.Forms.Button btnFive;
private System.Windows.Forms.Button btnSix;
private System.Windows.Forms.Button btnSeven;
private System.Windows.Forms.Button btnEight;
private System.Windows.Forms.Button btnNine;
private System.Windows.Forms.Button btnZero;
private System.Windows.Forms.Button btnDot;
private System.Windows.Forms.Button btnEqual;
private System.Windows.Forms.Button btnPlus;
private System.Windows.Forms.Button btnMinus;
private System.Windows.Forms.Button btnMulitply;
private System.Windows.Forms.Button btnDivide;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnSqrRoot;
private System.Windows.Forms.Button btnByTwo;
private System.Windows.Forms.Button btnByFour;
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace SimpleCalculator
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmCalculator());
}
}
}
feel free to ask if you have any doubt :)

More Related Content

Similar to C# Program. Create a Windows application that has the functionality .pdf

Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3Dillon Lee
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfindiaartz
 
This project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdfThis project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdfrajeshjangid1865
 
Lecture_7 Method Overloading.pptx
Lecture_7 Method Overloading.pptxLecture_7 Method Overloading.pptx
Lecture_7 Method Overloading.pptxMaheenVohra
 
안드로이드 세미나 2
안드로이드 세미나 2안드로이드 세미나 2
안드로이드 세미나 2ang0123dev
 
안드로이드 세미나 2
안드로이드 세미나 2안드로이드 세미나 2
안드로이드 세미나 2Chul Ju Hong
 
Import java
Import javaImport java
Import javaheni2121
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfudit652068
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA ProgramTrenton Asbury
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfadityastores21
 
Needs to be solved with Visual C# from the book Starting oout .pdf
Needs to be solved with Visual C# from the book Starting oout .pdfNeeds to be solved with Visual C# from the book Starting oout .pdf
Needs to be solved with Visual C# from the book Starting oout .pdffoottraders
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfsutharbharat59
 
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfCipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfravikapoorindia
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 

Similar to C# Program. Create a Windows application that has the functionality .pdf (20)

Integration Project Inspection 3
Integration Project Inspection 3Integration Project Inspection 3
Integration Project Inspection 3
 
This is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdfThis is a C# project . I am expected to create as this image shows. .pdf
This is a C# project . I am expected to create as this image shows. .pdf
 
This project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdfThis project should be done in C# using Visual Studio - Windows Form.pdf
This project should be done in C# using Visual Studio - Windows Form.pdf
 
Lecture_7 Method Overloading.pptx
Lecture_7 Method Overloading.pptxLecture_7 Method Overloading.pptx
Lecture_7 Method Overloading.pptx
 
안드로이드 세미나 2
안드로이드 세미나 2안드로이드 세미나 2
안드로이드 세미나 2
 
안드로이드 세미나 2
안드로이드 세미나 2안드로이드 세미나 2
안드로이드 세미나 2
 
Ditec esoft C# project
Ditec esoft C# projectDitec esoft C# project
Ditec esoft C# project
 
Ditec esoft C# project
Ditec esoft C# project Ditec esoft C# project
Ditec esoft C# project
 
Import java
Import javaImport java
Import java
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
final project for C#
final project for C#final project for C#
final project for C#
 
Exceptional exceptions
Exceptional exceptionsExceptional exceptions
Exceptional exceptions
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
 
Make sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdfMake sure to make a copy of the Google Doc for this lab into.pdf
Make sure to make a copy of the Google Doc for this lab into.pdf
 
Needs to be solved with Visual C# from the book Starting oout .pdf
Needs to be solved with Visual C# from the book Starting oout .pdfNeeds to be solved with Visual C# from the book Starting oout .pdf
Needs to be solved with Visual C# from the book Starting oout .pdf
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
 
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdfCipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
CipherDriver.javapackage Unit_6;import java.util.;public cl.pdf
 
SPF WinForm Programs
SPF WinForm ProgramsSPF WinForm Programs
SPF WinForm Programs
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 

More from fathimalinks

Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdffathimalinks
 
Write a program in C++ that declares a structure to store the code n.pdf
Write a program in C++ that declares a structure to store the code n.pdfWrite a program in C++ that declares a structure to store the code n.pdf
Write a program in C++ that declares a structure to store the code n.pdffathimalinks
 
Write a generic VBA Sub procedure to compute the value of the follow.pdf
Write a generic VBA Sub procedure to compute the value of the follow.pdfWrite a generic VBA Sub procedure to compute the value of the follow.pdf
Write a generic VBA Sub procedure to compute the value of the follow.pdffathimalinks
 
Why are standards needed in data communication and networking What .pdf
Why are standards needed in data communication and networking What .pdfWhy are standards needed in data communication and networking What .pdf
Why are standards needed in data communication and networking What .pdffathimalinks
 
Which of the following are mismatched A. Giordia - transmitted by f.pdf
Which of the following are mismatched  A. Giordia - transmitted by f.pdfWhich of the following are mismatched  A. Giordia - transmitted by f.pdf
Which of the following are mismatched A. Giordia - transmitted by f.pdffathimalinks
 
Which of the following statements about human evolution is correct.pdf
Which of the following statements about human evolution is correct.pdfWhich of the following statements about human evolution is correct.pdf
Which of the following statements about human evolution is correct.pdffathimalinks
 
What are the six major pollutions in the National Ambient Air Qualit.pdf
What are the six major pollutions in the National Ambient Air Qualit.pdfWhat are the six major pollutions in the National Ambient Air Qualit.pdf
What are the six major pollutions in the National Ambient Air Qualit.pdffathimalinks
 
UPS Worldpart DiscussionWhat do you think are the operational str.pdf
UPS Worldpart DiscussionWhat do you think are the operational str.pdfUPS Worldpart DiscussionWhat do you think are the operational str.pdf
UPS Worldpart DiscussionWhat do you think are the operational str.pdffathimalinks
 
True or False Justify your answer.Using multilevel signaling, it .pdf
True or False Justify your answer.Using multilevel signaling, it .pdfTrue or False Justify your answer.Using multilevel signaling, it .pdf
True or False Justify your answer.Using multilevel signaling, it .pdffathimalinks
 
This is question about excel cuers wish to answer shortly how to use.pdf
This is question about excel cuers wish to answer shortly how to use.pdfThis is question about excel cuers wish to answer shortly how to use.pdf
This is question about excel cuers wish to answer shortly how to use.pdffathimalinks
 
There is a requirement to design a system to sense the presence of gl.pdf
There is a requirement to design a system to sense the presence of gl.pdfThere is a requirement to design a system to sense the presence of gl.pdf
There is a requirement to design a system to sense the presence of gl.pdffathimalinks
 
The investments of Charger Inc. include a single investment 11,010 .pdf
The investments of Charger Inc. include a single investment 11,010 .pdfThe investments of Charger Inc. include a single investment 11,010 .pdf
The investments of Charger Inc. include a single investment 11,010 .pdffathimalinks
 
RNA polymerasebinds to DNA after the double strands have been unwoun.pdf
RNA polymerasebinds to DNA after the double strands have been unwoun.pdfRNA polymerasebinds to DNA after the double strands have been unwoun.pdf
RNA polymerasebinds to DNA after the double strands have been unwoun.pdffathimalinks
 
Research and explain the deviant actions of the Los Angeles Police D.pdf
Research and explain the deviant actions of the Los Angeles Police D.pdfResearch and explain the deviant actions of the Los Angeles Police D.pdf
Research and explain the deviant actions of the Los Angeles Police D.pdffathimalinks
 
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdfQuestion 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdffathimalinks
 
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdfQ1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdffathimalinks
 
Please revise the answer bellow.Q1. What historically have been Ap.pdf
Please revise the answer bellow.Q1. What historically have been Ap.pdfPlease revise the answer bellow.Q1. What historically have been Ap.pdf
Please revise the answer bellow.Q1. What historically have been Ap.pdffathimalinks
 
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdfNegligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdffathimalinks
 
List and explain at least three popular sixteenth century dance type.pdf
List and explain at least three popular sixteenth century dance type.pdfList and explain at least three popular sixteenth century dance type.pdf
List and explain at least three popular sixteenth century dance type.pdffathimalinks
 
Introduction to Database Management SystemConsider the following .pdf
Introduction to Database Management SystemConsider the following .pdfIntroduction to Database Management SystemConsider the following .pdf
Introduction to Database Management SystemConsider the following .pdffathimalinks
 

More from fathimalinks (20)

Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
 
Write a program in C++ that declares a structure to store the code n.pdf
Write a program in C++ that declares a structure to store the code n.pdfWrite a program in C++ that declares a structure to store the code n.pdf
Write a program in C++ that declares a structure to store the code n.pdf
 
Write a generic VBA Sub procedure to compute the value of the follow.pdf
Write a generic VBA Sub procedure to compute the value of the follow.pdfWrite a generic VBA Sub procedure to compute the value of the follow.pdf
Write a generic VBA Sub procedure to compute the value of the follow.pdf
 
Why are standards needed in data communication and networking What .pdf
Why are standards needed in data communication and networking What .pdfWhy are standards needed in data communication and networking What .pdf
Why are standards needed in data communication and networking What .pdf
 
Which of the following are mismatched A. Giordia - transmitted by f.pdf
Which of the following are mismatched  A. Giordia - transmitted by f.pdfWhich of the following are mismatched  A. Giordia - transmitted by f.pdf
Which of the following are mismatched A. Giordia - transmitted by f.pdf
 
Which of the following statements about human evolution is correct.pdf
Which of the following statements about human evolution is correct.pdfWhich of the following statements about human evolution is correct.pdf
Which of the following statements about human evolution is correct.pdf
 
What are the six major pollutions in the National Ambient Air Qualit.pdf
What are the six major pollutions in the National Ambient Air Qualit.pdfWhat are the six major pollutions in the National Ambient Air Qualit.pdf
What are the six major pollutions in the National Ambient Air Qualit.pdf
 
UPS Worldpart DiscussionWhat do you think are the operational str.pdf
UPS Worldpart DiscussionWhat do you think are the operational str.pdfUPS Worldpart DiscussionWhat do you think are the operational str.pdf
UPS Worldpart DiscussionWhat do you think are the operational str.pdf
 
True or False Justify your answer.Using multilevel signaling, it .pdf
True or False Justify your answer.Using multilevel signaling, it .pdfTrue or False Justify your answer.Using multilevel signaling, it .pdf
True or False Justify your answer.Using multilevel signaling, it .pdf
 
This is question about excel cuers wish to answer shortly how to use.pdf
This is question about excel cuers wish to answer shortly how to use.pdfThis is question about excel cuers wish to answer shortly how to use.pdf
This is question about excel cuers wish to answer shortly how to use.pdf
 
There is a requirement to design a system to sense the presence of gl.pdf
There is a requirement to design a system to sense the presence of gl.pdfThere is a requirement to design a system to sense the presence of gl.pdf
There is a requirement to design a system to sense the presence of gl.pdf
 
The investments of Charger Inc. include a single investment 11,010 .pdf
The investments of Charger Inc. include a single investment 11,010 .pdfThe investments of Charger Inc. include a single investment 11,010 .pdf
The investments of Charger Inc. include a single investment 11,010 .pdf
 
RNA polymerasebinds to DNA after the double strands have been unwoun.pdf
RNA polymerasebinds to DNA after the double strands have been unwoun.pdfRNA polymerasebinds to DNA after the double strands have been unwoun.pdf
RNA polymerasebinds to DNA after the double strands have been unwoun.pdf
 
Research and explain the deviant actions of the Los Angeles Police D.pdf
Research and explain the deviant actions of the Los Angeles Police D.pdfResearch and explain the deviant actions of the Los Angeles Police D.pdf
Research and explain the deviant actions of the Los Angeles Police D.pdf
 
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdfQuestion 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
 
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdfQ1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf
Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf
 
Please revise the answer bellow.Q1. What historically have been Ap.pdf
Please revise the answer bellow.Q1. What historically have been Ap.pdfPlease revise the answer bellow.Q1. What historically have been Ap.pdf
Please revise the answer bellow.Q1. What historically have been Ap.pdf
 
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdfNegligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
 
List and explain at least three popular sixteenth century dance type.pdf
List and explain at least three popular sixteenth century dance type.pdfList and explain at least three popular sixteenth century dance type.pdf
List and explain at least three popular sixteenth century dance type.pdf
 
Introduction to Database Management SystemConsider the following .pdf
Introduction to Database Management SystemConsider the following .pdfIntroduction to Database Management SystemConsider the following .pdf
Introduction to Database Management SystemConsider the following .pdf
 

Recently uploaded

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
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 '+':
  • 4. //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(object sender, EventArgs e) { operand1 = txtInput.Text;
  • 5. operation = '/'; txtInput.Text = string.Empty; } private void btnEqual_Click(object sender, EventArgs e) { operand2 = txtInput.Text; double opr1, opr2; double.TryParse(operand1, out opr1); double.TryParse(operand2, out opr2); switch (operation) { case '+': result = (opr1 + opr2).ToString(); break; case '-': result = (opr1 - opr2).ToString(); break; case '*': result = (opr1 * opr2).ToString(); break; case '/': if (opr2 != 0) { result = (opr1 / opr2).ToString(); } else { MessageBox.Show("Can't divide by zero"); } break; } txtInput.Text = result.ToString(); } private void btnClear_Click(object sender, EventArgs e) { txtInput.Text = string.Empty;
  • 6. operand1 = string.Empty; operand2 = string.Empty; } private void btnSqrRoot_Click(object sender, EventArgs e) { double opr1; if (double.TryParse(txtInput.Text, out opr1)) { txtInput.Text = (Math.Sqrt(opr1)).ToString(); } } private void btnByTwo_Click(object sender, EventArgs e) { double opr1; if (double.TryParse(txtInput.Text, out opr1)) { txtInput.Text = (opr1 / 2).ToString(); } } private void btnByFour_Click(object sender, EventArgs e) { double opr1; if (double.TryParse(txtInput.Text, out opr1)) { txtInput.Text = (opr1 / 4).ToString(); } } } } Form1.Designer.cs namespace SimpleCalculator { partial class frmCalculator { ///
  • 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;
  • 9. // // btnThree // this.btnThree.Location = new System.Drawing.Point(92, 39); this.btnThree.Name = "btnThree"; this.btnThree.Size = new System.Drawing.Size(34, 23); this.btnThree.TabIndex = 3; this.btnThree.Text = "3"; this.btnThree.UseVisualStyleBackColor = true; // // btnFour // this.btnFour.Location = new System.Drawing.Point(12, 68); this.btnFour.Name = "btnFour"; this.btnFour.Size = new System.Drawing.Size(34, 23); this.btnFour.TabIndex = 4; this.btnFour.Text = "4"; this.btnFour.UseVisualStyleBackColor = true; // // btnFive // this.btnFive.Location = new System.Drawing.Point(52, 68); this.btnFive.Name = "btnFive"; this.btnFive.Size = new System.Drawing.Size(34, 23); this.btnFive.TabIndex = 5; this.btnFive.Text = "5"; this.btnFive.UseVisualStyleBackColor = true; // // btnSix // this.btnSix.Location = new System.Drawing.Point(92, 68); this.btnSix.Name = "btnSix"; this.btnSix.Size = new System.Drawing.Size(34, 23); this.btnSix.TabIndex = 6; this.btnSix.Text = "6"; this.btnSix.UseVisualStyleBackColor = true;
  • 10. // // btnSeven // this.btnSeven.Location = new System.Drawing.Point(13, 97); this.btnSeven.Name = "btnSeven"; this.btnSeven.Size = new System.Drawing.Size(34, 23); this.btnSeven.TabIndex = 7; this.btnSeven.Text = "7"; this.btnSeven.UseVisualStyleBackColor = true; // // btnEight // this.btnEight.Location = new System.Drawing.Point(53, 97); this.btnEight.Name = "btnEight"; this.btnEight.Size = new System.Drawing.Size(34, 23); this.btnEight.TabIndex = 8; this.btnEight.Text = "8"; this.btnEight.UseVisualStyleBackColor = true; // // btnNine // this.btnNine.Location = new System.Drawing.Point(93, 97); this.btnNine.Name = "btnNine"; this.btnNine.Size = new System.Drawing.Size(34, 23); this.btnNine.TabIndex = 9; this.btnNine.Text = "9"; this.btnNine.UseVisualStyleBackColor = true; // // btnZero // this.btnZero.Location = new System.Drawing.Point(13, 126); this.btnZero.Name = "btnZero"; this.btnZero.Size = new System.Drawing.Size(34, 23); this.btnZero.TabIndex = 10; this.btnZero.Text = "0"; this.btnZero.UseVisualStyleBackColor = true;
  • 11. // // btnDot // this.btnDot.Location = new System.Drawing.Point(52, 126); this.btnDot.Name = "btnDot"; this.btnDot.Size = new System.Drawing.Size(34, 23); this.btnDot.TabIndex = 11; this.btnDot.Text = "."; this.btnDot.UseVisualStyleBackColor = true; // // btnEqual // this.btnEqual.Location = new System.Drawing.Point(93, 126); this.btnEqual.Name = "btnEqual"; this.btnEqual.Size = new System.Drawing.Size(34, 23); this.btnEqual.TabIndex = 12; this.btnEqual.Text = "="; this.btnEqual.UseVisualStyleBackColor = true; this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click); // // btnPlus // this.btnPlus.Location = new System.Drawing.Point(132, 39); this.btnPlus.Name = "btnPlus"; this.btnPlus.Size = new System.Drawing.Size(34, 23); this.btnPlus.TabIndex = 13; this.btnPlus.Text = "+"; this.btnPlus.UseVisualStyleBackColor = true; this.btnPlus.Click += new System.EventHandler(this.btnPlus_Click); // // btnMinus // this.btnMinus.Location = new System.Drawing.Point(132, 68); this.btnMinus.Name = "btnMinus"; this.btnMinus.Size = new System.Drawing.Size(34, 23); this.btnMinus.TabIndex = 14;
  • 12. this.btnMinus.Text = "-"; this.btnMinus.UseVisualStyleBackColor = true; this.btnMinus.Click += new System.EventHandler(this.btnMinus_Click); // // btnMulitply // this.btnMulitply.Location = new System.Drawing.Point(132, 97); this.btnMulitply.Name = "btnMulitply"; this.btnMulitply.Size = new System.Drawing.Size(34, 23); this.btnMulitply.TabIndex = 15; this.btnMulitply.Text = "*"; this.btnMulitply.UseVisualStyleBackColor = true; this.btnMulitply.Click += new System.EventHandler(this.btnMulitply_Click); // // btnDivide // this.btnDivide.Location = new System.Drawing.Point(132, 126); this.btnDivide.Name = "btnDivide"; this.btnDivide.Size = new System.Drawing.Size(34, 23); this.btnDivide.TabIndex = 16; this.btnDivide.Text = "/"; this.btnDivide.UseVisualStyleBackColor = true; this.btnDivide.Click += new System.EventHandler(this.btnDivide_Click); // // btnClear // this.btnClear.Location = new System.Drawing.Point(172, 39); this.btnClear.Name = "btnClear"; this.btnClear.Size = new System.Drawing.Size(34, 23); this.btnClear.TabIndex = 17; this.btnClear.Text = "C"; this.btnClear.UseVisualStyleBackColor = true; this.btnClear.Click += new System.EventHandler(this.btnClear_Click); // // btnSqrRoot //
  • 13. this.btnSqrRoot.Location = new System.Drawing.Point(172, 68); this.btnSqrRoot.Name = "btnSqrRoot"; this.btnSqrRoot.Size = new System.Drawing.Size(34, 23); this.btnSqrRoot.TabIndex = 18; this.btnSqrRoot.Text = ""; this.btnSqrRoot.UseVisualStyleBackColor = true; this.btnSqrRoot.Click += new System.EventHandler(this.btnSqrRoot_Click); // // btnByTwo // this.btnByTwo.Location = new System.Drawing.Point(172, 97); this.btnByTwo.Name = "btnByTwo"; this.btnByTwo.Size = new System.Drawing.Size(34, 23); this.btnByTwo.TabIndex = 19; this.btnByTwo.Text = "½"; this.btnByTwo.UseVisualStyleBackColor = true; this.btnByTwo.Click += new System.EventHandler(this.btnByTwo_Click); // // btnByFour // this.btnByFour.Location = new System.Drawing.Point(172, 126); this.btnByFour.Name = "btnByFour"; this.btnByFour.Size = new System.Drawing.Size(34, 23); this.btnByFour.TabIndex = 20; this.btnByFour.Text = "¼"; this.btnByFour.UseVisualStyleBackColor = true; this.btnByFour.Click += new System.EventHandler(this.btnByFour_Click); // // frmCalculator // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(214, 157); this.Controls.Add(this.btnByFour); this.Controls.Add(this.btnByTwo); this.Controls.Add(this.btnSqrRoot);
  • 14. this.Controls.Add(this.btnClear); this.Controls.Add(this.btnDivide); this.Controls.Add(this.btnMulitply); this.Controls.Add(this.btnMinus); this.Controls.Add(this.btnPlus); this.Controls.Add(this.btnEqual); this.Controls.Add(this.btnDot); this.Controls.Add(this.btnZero); this.Controls.Add(this.btnNine); this.Controls.Add(this.btnEight); this.Controls.Add(this.btnSeven); this.Controls.Add(this.btnSix); this.Controls.Add(this.btnFive); this.Controls.Add(this.btnFour); this.Controls.Add(this.btnThree); this.Controls.Add(this.btnTwo); this.Controls.Add(this.btnOne); this.Controls.Add(this.txtInput); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "frmCalculator"; this.Text = "Simple Calculator"; this.Load += new System.EventHandler(this.frmCalculator_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txtInput; private System.Windows.Forms.Button btnOne; private System.Windows.Forms.Button btnTwo; private System.Windows.Forms.Button btnThree; private System.Windows.Forms.Button btnFour; private System.Windows.Forms.Button btnFive; private System.Windows.Forms.Button btnSix; private System.Windows.Forms.Button btnSeven;
  • 15. private System.Windows.Forms.Button btnEight; private System.Windows.Forms.Button btnNine; private System.Windows.Forms.Button btnZero; private System.Windows.Forms.Button btnDot; private System.Windows.Forms.Button btnEqual; private System.Windows.Forms.Button btnPlus; private System.Windows.Forms.Button btnMinus; private System.Windows.Forms.Button btnMulitply; private System.Windows.Forms.Button btnDivide; private System.Windows.Forms.Button btnClear; private System.Windows.Forms.Button btnSqrRoot; private System.Windows.Forms.Button btnByTwo; private System.Windows.Forms.Button btnByFour; } } Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace SimpleCalculator { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmCalculator()); } } }
  • 16. feel free to ask if you have any doubt :)