TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Calculator In with a Flat Design C#
Bruno Kiafuka
May 16
2016
Code
1.8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Calculator.zip
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Runtime.InteropServices;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace
Calculator
{
public
partial
class
Form1: Form
{
public
Form1()
{
InitializeComponent();
}
//vars
string
oper;
decimal
num1, num2, result = 0;
private
void
Form1_Load(
object
sender, EventArgs e)
{
// txtDisplay.Text = "0";
}
//1
private
void
btn1_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn1.Text;
}
//2
private
void
btn2_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn2.Text;
}
//3
private
void
btn3_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn3.Text;
}
//4
private
void
btn4_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn4.Text;
}
//5
private
void
btn5_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn5.Text;
}
//6
private
void
btn6_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn6.Text;
}
//7
private
void
btn7_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn7.Text;
}
//8
private
void
btn8_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn8.Text;
}
//9
private
void
btn9_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn9.Text;
}
//0
private
void
btn0_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btn0.Text;
}
//,
private
void
btnComma_Click(
object
sender, EventArgs e)
{
txtDisplay.Text += btnComma.Text;
}
// +|-
private
void
btnPlusOrMinus_Click(
object
sender, EventArgs e)
{
if
(txtDisplay.Text.Contains(
"-"
))
{
txtDisplay.Text = txtDisplay.Text.Remove(0, 1);
}
else
{
txtDisplay.Text =
"-"
+ txtDisplay.Text;
}
}
//Plus
private
void
btnPlus_Click(
object
sender, EventArgs e)
{
num1 =
decimal
.Parse(txtDisplay.Text);
oper =
"+"
;
txtDisplay.Text =
string
.Empty;
textBox1.Text = num1.ToString() +
" + "
;
}
//SUB
private
void
btnSub_Click(
object
sender, EventArgs e)
{
num1 =
decimal
.Parse(txtDisplay.Text);
oper =
"-"
;
txtDisplay.Text =
string
.Empty;
textBox1.Text = num1.ToString() +
" - "
;
}
//multiply
private
void
btnTimes_Click(
object
sender, EventArgs e)
{
num1 =
decimal
.Parse(txtDisplay.Text);
oper =
"x"
;
txtDisplay.Text =
string
.Empty;
textBox1.Text = num1.ToString() +
" x "
;
}
//divide
private
void
btnDivide_Click(
object
sender, EventArgs e)
{
num1 =
decimal
.Parse(txtDisplay.Text);
oper =
"/"
;
txtDisplay.Text =
string
.Empty;
textBox1.Text = num1.ToString() +
" / "
;
}
private
void
btnEqual_Click(
object
sender, EventArgs e)
{
num2 =
decimal
.Parse(txtDisplay.Text);
textBox1.Text =
string
.Empty;
switch
(oper)
{
case
"+"
:
result = num1 + num2;
txtDisplay.Text = result.ToString();
break
;
case
"-"
:
result = num1 - num2;
txtDisplay.Text = result.ToString();
break
;
case
"x"
:
result = num1 * num2;
txtDisplay.Text = result.ToString();
break
;
case
"/"
:
if
(num2 == 0)
{
MessageBox.Show(num1 +
" cannot be divided by zero"
,
"Error"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtDisplay.Text =
"0"
;
}
else
{
result = num1 / num2;
txtDisplay.Text = result.ToString();
}
break
;
case
"%"
:
result = num1 % num2;
txtDisplay.Text = result.ToString();
break
;
}
}
//x^2
private
void
btnXsquare_Click(
object
sender, EventArgs e)
{
result =
decimal
.Parse(txtDisplay.Text) *
decimal
.Parse(txtDisplay.Text);
textBox1.Text = txtDisplay.Text +
" ^ "
+ 2 +
" "
;
txtDisplay.Text = result.ToString();
}
//%
private
void
btnPercentage_Click(
object
sender, EventArgs e)
{
num1 =
decimal
.Parse(txtDisplay.Text);
oper =
"%"
;
txtDisplay.Text =
string
.Empty;
textBox1.Text = num1.ToString() +
" % "
;
}
private
void
btnErease_Click(
object
sender, EventArgs e)
{
string
str1 = txtDisplay.Text;
int
n = str1.Length;
txtDisplay.Text = (str1.Substring(0, n - 1));
}
private
void
btnC_Click(
object
sender, EventArgs e)
{
txtDisplay.Text =
string
.Empty;
textBox1.Text =
string
.Empty;
}
private
void
btnCE_Click(
object
sender, EventArgs e)
{
txtDisplay.Text =
"0"
;
textBox1.Text =
string
.Empty;
}
// dragg form
public
const
int
WM_NCLBUTTONDOWN = 0xA1;
public
const
int
HT_CAPTION = 0x2;
[DllImportAttribute(
"user32.dll"
)]
public
static
extern
int
SendMessage(IntPtr hWnd,
int
Msg,
int
wParam,
int
lParam);
[DllImportAttribute(
"user32.dll"
)]
public
static
extern
bool
ReleaseCapture();
private
void
topPanel_MouseDown(
object
sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(
this
.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
private
void
btn1x_Click(
object
sender, EventArgs e)
{
result = 1 /
decimal
.Parse(txtDisplay.Text);
textBox1.Text = 1 +
" / "
+ txtDisplay.Text +
" "
;
txtDisplay.Text = result.ToString();
}
private
void
btnV_Click(
object
sender, EventArgs e)
{
double
num = Math.Sqrt(
double
.Parse(txtDisplay.Text));
textBox1.Text =
"√ "
+ txtDisplay.Text;
result = Convert.ToDecimal(num);
txtDisplay.Text = result.ToString();
}
private
void
btnClose_Click(
object
sender, EventArgs e)
{
Application.Exit();
}
private
void
btnMin_Click(
object
sender, EventArgs e)
{
this
.WindowState = FormWindowState.Minimized;
}
//..
}
}
//Author: Bruno Kiafuka
C# Calculator