|
The
Form design, calculator.cs
Create Visual C# 2010 Forn -
calculator.cs
Classes used in this Form: ListBox, TextBox,
Button
Create the Form
calculator.cs
Add New Item -
calculator.cs Form to work_VCNet10
Project
- On the Projet
menu , click Add New Item ...,
Add New Item - work_VCNet10 dialog box appears, Select
Windows form Icon,
in the Name box type
calculator.cs and then click
Add.
|
 |
|
Note:
In the Windows Forms Designer appears the
design form created and in the Properties
Windows displays the corresponding properties
and
The design Form calculator.cs created, appears the empty window Form
design |
|
The codes file, created
... |
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
work_VCNet10
{
public
partial
class
calculator :
Form
{
public
calculator()
{
InitializeComponent();
}
private
void calculator_Load(object
sender,
EventArgs
e)
{
}
}
}
|
|
|
|
|
|
|
1. |
The
calculator.cs
design |
|
 |
The Properties of
the Form -
calculator.cs... |
Name: Icon: Maximize: Menu: Size: Start
Position: Text: Window
State: |
calculator Icon False (none) 368,332 CenterScreen calculator
... Normal | | | |
3. |
From the Toolbox/Windows Form add these
controls .... 1
ListBox contol,
1 TextBox contol,
21 Button contols
|
|
- Add
the ListBox
control - ListBox1,
ListBox
Properties:
Name: Size
: |
ListBox1 176,
172 | |
- Add
the TextBox
control - CalcField
Properties:
Name: Font: Size: Text: |
CalcField Times
New Roman, 8.25pt 184,
20 | |
- Add
21 Button
controls
- Btndetail ,
Button
Properties
Name: BackColor: Size: Text: |
Btndetail Silver 50,
20 detail | |
- CalcPlus -
Size: 40,
32
- CalcSub
-
Size: 40,
32
- CalcMul
-
Size: 40,
32
- CalcDiv
-
Size: 40,
32
- CalcDec
-
Size: 40,
32
- CalcSign
- Size: 40,
32
- CalcRes
-
Size: 40,
32
- CalcCan
-
Size: 50,
32
- CalcCE
-
Size: 50,
32
- CalcBS
-
Size:
96,
32
- Calc0
... Calc9 (
10
Button controls
)
-
Size: 40,
32
| | | |
|
calculator.cs file, the codes after modification
...
|
|
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
work_VCnet10
{
public
partial
class
calculator
:
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 calculator()
{
InitializeComponent();
//Get decimal separator based upon machine local settings.
mDecValue = System.Globalization. NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
this.CalcDec.Text =
mDecValue;
mMinus =
"-";
}
private
void
calculator_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.Calc6.Left * 1
/ 2;
this.Btndetail.Text
=
"detail";
}
private
void
calculator_Activated(object
sender,
EventArgs
e)
{
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.Items.Clear();
FirstFlag =
true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
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 = Value + mDecValue;
}
else
{
if (Value ==
"0")
Value = NewChar;
else
Value = 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 = 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 = mOp1 + mOp2;
break;
case
"-":
mOp1 = mOp1 - mOp2;
break;
case
"*":
mOp1 = 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 = 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 = 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;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
//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;
}
}
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);
this.Btndetail.Text
=
"hide";
}
else
if
(this.Btndetail.Text
==
"hide")
{
this.Width =
this.ListBox1.Left
+
this.Calc6.Left * 1
/ 2;
this.Btndetail.Text
=
"detail";
}
}
}
}
| | |
|
|