|
 |
Calculator-C Window Application
developed with Microsoft Visual C#
2019 | |
 |
|
|
|
|
|
|
|
The Microsoft Visual C# 2019 member of the Microsoft
Visual Studio Community 2019 |
|
Calculator-C
Window Application.., describe
how to use Microsoft Visual C# 2019 to create application
1- |
Calculator-C Executable (exe file)
| |
|
-
Calculator
Software Application,
-
Functions:
calculator, Animation
, Information data, about
your PC ...
-
Executable
Calculator
(exe file), Developed with
Microsoft Visual C# 2019
|
 | |
|
|
2- |
The
Microsoft Visual C# 2019 member of the Microsoft
Visual Studio Community 2019 |
|
|
3- |
How Create
a C# application project. |
|
-
Open Visual Studio 2019.
-
On the start window, choose Create
a new project.
-
On the Create
a new project window, choose
the Windows
Forms App (.NET Framework) template for
C#.
(If you prefer, you can refine your search to quickly get to
the template you want. For example, enter or type Windows
Forms App in the search box. Next,
choose C# from
the Language list, and then choose Windows from
the Platform list.)
-
In the Configure
your new project window, type or
enter HelloWorld in
the Project
name box. Then,
choose Create.
-
Visual Studio opens your new project.
-
Create the application After you
select your C# project template and name your file, Visual Studio
opens a form for you. A form is a Windows user interface.
We'll create a "Hello World" application by adding controls
to the form, and then we'll run the application.
-
Note:
the Name of the
default Form created - Form1.cs, Before to Add controls,
it is Available to Modify the name and Proprieties of this
Form
-
Add
a "Controls" to this form ... |
|
|
4- |
Calculator-C Proprieties and
Solution
Explorer |
|
- Calculator-C
Proprieties:
- the
Assembly/Project name :
Calculator-C
-
Assembly Project Folder:
C:\Users\...\source\repos\Calculator-C\
-
the Target framework: .Net Framework
4.7.2
- the Output
type: Window Application
- Startup
object: Calculator-C.Program
|
 |
-
Calculator-C Solution Explorer
- Calculator-C
project developed with C# 2019, consist
of:
- 2
Form files -
Form1.cs,
AboutC.cs
- 1
Folder - pic
pic folder stores 3 pictures files
|
|
 |
|
|
|
5- |
How
to Run this
application
|
|
-
Remove the read-only
attribute of all
files in the folder -
C:\Users\...\source\repos\Calculator-C
- Run
the Microsoft Visual Studio
Community 2019.
- From
File menu, choose
Open
then
choose Project/Solution,
the Open Project dialog
box appears, select the
Calculator-C
project file -Type:
Microsoft Visual Studio
Solution
(C:\Users\...\source\repos\Calculator-C
\Calculator-C .sln)
and then click Open.
- From
Debug menu, choose
and click Start
Debugging
|
|
|
6- |
After
running Calculator-C, the following View design
display by order |
|
|
 |
Calculator-C Application developed with
Microsoft Visual Studio Community 2019, C#
| |
Calculator-C Application developed with
Microsoft Visual Studio Community 2019, C# |
1. |
-
Calculator Software
-
Operations: calculator, Animation
, Information data, about
your PC ...
-
Includes 2 Forms
-
Classes used :System
Windows Forms, ImageList, Label,, Timer
|
2. |
1st Form -
Form1.cs |
|
The design
|
 |
Functions
|
Calculator functions
with 2 windows .... | |
3. |
3th Form
- AboutC.cs |
|
The design
|
 |
Functions
|
Animation
General Info
....
Information data, about
your PC
| | | | | |
|
|
|
|
|
|
7- |
Form1.cs,
1st
Form of Calculator-C project/Application |
|
7.1 |
View
Designer |
|
 |
|
|
7.2 |
View
Codes |
|
|
 |
the
Code of Form1
Form | |
the Code of Form1
Form of
Calculator-C Application developed
with Microsoft Visual Studio Community 2019, C# |
|
|
|
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 Calculator_C
{
public partial class Form1 : Form
{
//variables
double mOp1, mOp2; //Previously input operand.
int mNumOps; //Number of operands.
Operation mLastInput; //Indicate type of last
keypress event.
string mOpFlag; //Indicate pending operation.
string mOpPrev; //Previous operation
string mMinus; //Minus operator "-"
string MDecValue;
bool mAllowBackSpace; //Allow backspace
bool FirstFlag;
enum Operation
{
None = 0,
Operand = 1,
Operator = 2,
CE = 3,
Cancel = 4
}
public Form1()
{
InitializeComponent();
//Get decimal separator based upon machine local
settings.
MDecValue =
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
this.CalcDec.Text = MDecValue;
mMinus = "-";
}
private void Form1_Activated(object sender,
EventArgs e)
{
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.Items.Clear();
FirstFlag = true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void Form1_Load(object sender, EventArgs
e)
{
this.Btndetail.Left = this.CalcCan.Left;
this.Btndetail.Top = this.CalcField.Top +
this.CalcField.Height - this.Btndetail.Height;
this.ListBox1.Top = this.CalcField.Top; // + (((this.CalcBS.Top
+ this.CalcBS.Height) - this.CalcField.Top) / 2)
- (this.ListBox1.Height / 2);
this.ListBox1.Left = this.CalcCan.Left +
this.CalcCan.Width + (this.Calc6.Left * 5 / 4);
this.Width = this.ListBox1.Left +
this.ListBox1.Width + (this.Calc6.Left * 2) + 5;
this.Btndetail.Text = "Hide";
this.ButtonA.Left = this.ListBox1.Left +
(this.Calc6.Left);
this.ButtonE.Left = this.ListBox1.Left +
this.ListBox1.Width - (this.ButtonE.Width +
(this.Calc6.Left));
}
private void Calc0_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void CalcNum_Click(object sender,
EventArgs e)
{
System.Windows.Forms.Button SelButton;
mAllowBackSpace = true; //now allow backspace
if (mLastInput != Operation.Operand)
this.CalcField.Text = "0";
SelButton = (Button)sender;
FormatEditField(SelButton.Text);
mLastInput = Operation.Operand;
}
//format the entry in the text box!
private void FormatEditField(string NewChar)
{
string Value;
Value = this.CalcField.Text;
//Determine if there are more than one .'s
if (NewChar == MDecValue)
{
//If it's found.
this.richTextData.Text = Value;
if (this.richTextData.Find(MDecValue) > -1)
//Don't do anything.
return;
Value += MDecValue;
}
else
{
if (Value == "0")
Value = NewChar;
else
Value += NewChar;
}
this.CalcField.Text = Value;
}
private void Calc1_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc2_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc3_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc4_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc5_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc6_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc7_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc8_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void Calc9_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void CalcDec_Click(object sender,
EventArgs e)
{
this.CalcNum_Click(sender, e);
}
private void CalcPlus_Click(object sender,
EventArgs e)
{
this.CalcRes_Click(sender, e);
}
//Handles +, -, *, /, =
private void CalcRes_Click(object sender,
EventArgs e)
{
System.Windows.Forms.Button ButtonPressed;
string vstr;
string vline;
int i;
vline = "-";
//Now do not allow backspace(CalcBS) actions
after the result has been
//calculated and displayed.
mAllowBackSpace = false;
if (mLastInput.Equals(Operation.Operand))
mNumOps += 1;
switch (mNumOps)
{
case 1:
if (this.ListBox1.Items.Count == 0)
{
this.ListBox1.Items.Add(CalcField.Text);
}
mOp1 = Double.Parse(CalcField.Text);
break;
case 2:
mOp2 = Double.Parse(CalcField.Text);
switch (mOpFlag)
{
case "+":
mOp1 += mOp2;
break;
case "-":
mOp1 -= mOp2;
break;
case "*":
mOp1 *= mOp2;
break;
case "/":
if (mOp2 == 0)
MessageBox.Show("Can't divide by zero! ...",
"work_VCnet10.mainform.mainform_cl.Title",
MessageBoxButtons.OK, MessageBoxIcon.Information);
else
mOp1 /= mOp2;
break;
case "=":
mOp1 = mOp2;
break;
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if (mOpFlag == "=")
{
}
else
{
this.ListBox1.Items.Add(mOpFlag);
if (mNumOps == 2)
this.ListBox1.Items.Add(" " + mOp2.ToString());
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.CalcField.Text = mOp1.ToString();
mNumOps = 1;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if (mOpFlag == "=")
{
vstr = " " + ".......... new operation
..........";
this.ListBox1.Items.Add(vstr);
vstr = " " + mOp1.ToString();
this.ListBox1.Items.Add(vstr);
this.ListBox1.SelectedIndex =
ListBox1.Items.Count - 1;
}
else
{
vstr = mOp1.ToString();
for (i = 0; i <= (vstr.Length * 2) - 4; i++)
{
vline += "-";
}
this.ListBox1.Items.Add(" " + vline);
this.ListBox1.Items.Add(" " + mOp1.ToString());
this.ListBox1.SelectedIndex =
ListBox1.Items.Count - 1;
}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
break;
}
mLastInput = Operation.Operator;
mOpPrev = mOpFlag;
ButtonPressed = (Button)sender;
mOpFlag = ButtonPressed.Text;
//
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
if (FirstFlag == true)
{
vstr = " " + ".......... new operation
..........";
this.ListBox1.Items.Add(vstr);
if (mNumOps == 1)
{
vstr = " " + mOp1;
this.ListBox1.Items.Add(vstr);
}
}
FirstFlag = false;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void CalcMul_Click(object sender,
EventArgs e)
{
this.CalcRes_Click(sender, e);
}
private void CalcDiv_Click(object sender,
EventArgs e)
{
this.CalcRes_Click(sender, e);
}
private void CalcSub_Click(object sender,
EventArgs e)
{
this.CalcRes_Click(sender, e);
}
//+/- click event.
private void CalcSign_Click(object sender,
EventArgs e)
{
if (mLastInput != Operation.Operand)
this.CalcField.Text = "0";
else
{
if (this.CalcField.Text.Substring(0, 1) ==
mMinus)
this.CalcField.Text =
this.CalcField.Text.Substring(1);
else
{
if (this.CalcField.Text != "0")
this.CalcField.Text = mMinus +
this.CalcField.Text;
}
}
mLastInput = Operation.Operand;
}
//Cancel click event.
private void CalcCan_Click(object sender,
EventArgs e)
{
Reset();
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.Items.Clear();
FirstFlag = true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
// Helper to initialize the vals.
private void Reset()
{
mOp1 = 0;
mOp2 = 0;
mNumOps = 0;
mLastInput = Operation.None;
mOpFlag = "";
mOpPrev = "";
this.CalcField.Text = "0";
mAllowBackSpace = true;
}
//Cancel entry click event.
private void CalcCE_Click(object sender,
EventArgs e)
{
if (mLastInput.Equals(Operation.Operand))
this.CalcField.Text = "0";
else
{
if (mLastInput.Equals(Operation.Operator))
mOpFlag = mOpPrev;
}
mLastInput = Operation.CE;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.Items.Clear();
FirstFlag = true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void Btndetail_Click(object sender,
EventArgs e)
{
if (this.Btndetail.Text == "Detail")
{
this.Width = this.ListBox1.Left +
this.ListBox1.Width + (this.Calc6.Left * 2) + 5;
this.Btndetail.Text = "Hide";
}
else if (this.Btndetail.Text == "Hide")
{
this.Width = this.CalcCan.Left +
this.CalcCan.Width + (this.Calc6.Left * 2) + 5;
this.Btndetail.Text = "Detail";
}
}
private void ButtonE_Click(object sender,
EventArgs e)
{
Application.Exit();
}
private void ButtonA_Click(object sender,
EventArgs e)
{
AboutC FAC = new AboutC();
FAC.Show();
}
//BS click event.
private void CalcBS_Click(object sender,
EventArgs e)
{
string Value;
//Check if we can backspace before removing
items from CalcField.text.
if (mAllowBackSpace == true)
{
Value = this.CalcField.Text;
if (Value.Length > 1)
///Value = Mid(Value, 1, Value.Length - 1);
Value = Value.Substring(0, (Value.Length - 1));
else
Value = "0";
//if all we are left is the minus sign, reset.
if (Value == "-")
Value = "0";
this.CalcField.Text = Value;
}
}
}
} |
|
| | | |
|
| |
|
|
8- |
AboutC.cs,
2nd
Form of Calculator-C
project/Application |
|
8.1 |
View
Designer |
|
 |
|
|
8.2 |
View
Codes |
|
|
 |
the
Code of AboutC
Form | |
the Code of AboutC.cs
Form of
Calculator-C Application developed with
Microsoft Visual Studio Community 2019, C# |
|
|
|
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
Calculator_C
{
public
partial
class
AboutC
: Form
{
int
IcoNmbr = 0;
int
VFHeight;
public
AboutC()
{
InitializeComponent();
}
private
void
Timer1_Tick(object
sender, EventArgs e)
{
if
(IcoNmbr == 0)
{
this.picico.Image
=
this.picicoOrg0.Image;
IcoNmbr = 1;
}
else
{
this.picico.Image
=
this.picicoOrg1.Image;
IcoNmbr = 0;
}
}
private
void
AboutC_Load(object
sender, EventArgs e)
{
this.Btndetail.Text
=
"Hide";
VFHeight =
this.Height;
this.TextBox1.Text
=
System.Environment.MachineName.ToString();
this.TextBox2.Text
=
System.Environment.UserName.ToString();
this.TextBox3.Text
=
System.Environment.OSVersion.ToString();
this.TextBox4.Text
=
System.Environment.Version.ToString();
this.TextBox5.Text
=
System.Environment.SystemDirectory.ToString();
this.picico.Select();
this.picico.Image
=
this.picicoOrg0.Image;
IcoNmbr = 1;
}
private
void
Btndetail_Click(object
sender, EventArgs e)
{
if
(this.Btndetail.Text
==
"Detail")
{
this.Height
= VFHeight;
this.Btndetail.Text
=
"Hide";
}
else
if
(this.Btndetail.Text
==
"Hide")
{
this.Height
=
this.BtnDone.Top
+ (this.BtnDone.Height
* 3);
this.Btndetail.Text
=
"Detail";
}
}
private
void
btnDone_Click(object
sender, EventArgs e)
{
this.Close();
}
}
} |
|
| | | |
|
| |
|
|
|
|
|
| | | |