|
The Form design,
calculator.cs
Create Visual C# .Net Forn - calculator.cs
Classes used in this
Form: ListBox, TextBox,
Button
- On the View menu ,
click Solution Explorer
- In Solution
Explorer
In Solution Explorer, right click the
work_VCnet, select Add on the shortcut menu to
open other shortcut menu, click Add New Item,
the Add New Item - work_VCnet dialog box
appears. |

|
- In the Add New
Item -work_VCnet dialog box:
- In the
Categories pane, select Local Project
Items
- In the Templates
pane, select Windows form . A message
appears - (a form for Windows Applications).
- In the Name
box, type calculator.cs
- Click
Open
|

|
- In the windows
Forms Designer appears the empty window Form
design
|
|
|
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
...
The
text
Code
is red color, the codes added manuel |
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace work_VCnet
{
///
<summary>
///
Summary description for calculator.
///
</summary>
public
class
calculator : System.Windows.Forms.Form
{
internal
System.Windows.Forms.Button Btndetail;
internal
System.Windows.Forms.ListBox ListBox1;
internal
System.Windows.Forms.TextBox CalcField;
internal
System.Windows.Forms.Button CalcRes;
internal
System.Windows.Forms.Button CalcPlus;
internal
System.Windows.Forms.Button CalcDec;
internal
System.Windows.Forms.Button CalcSign;
internal
System.Windows.Forms.Button Calc0;
internal
System.Windows.Forms.Button CalcCan;
internal
System.Windows.Forms.Button CalcSub;
internal
System.Windows.Forms.Button Calc3;
internal
System.Windows.Forms.Button Calc2;
internal
System.Windows.Forms.Button Calc1;
internal
System.Windows.Forms.Button CalcCE;
internal
System.Windows.Forms.Button CalcMul;
internal
System.Windows.Forms.Button Calc6;
internal
System.Windows.Forms.Button Calc5;
internal
System.Windows.Forms.Button Calc4;
internal
System.Windows.Forms.Button CalcBS;
internal
System.Windows.Forms.Button CalcDiv;
internal
System.Windows.Forms.Button Calc9;
internal
System.Windows.Forms.Button Calc8;
internal
System.Windows.Forms.Button Calc7;
private
System.Windows.Forms.RichTextBox richTextData;
///
<summary>
///
Required designer variable.
///
</summary>
private
System.ComponentModel.Container components =
null;
//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()
{
//
// Required for
Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any
constructor code after InitializeComponent call
//
//Get decimal
separator based upon machine local settings.
mDecValue =
System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
this.CalcDec.Text
= mDecValue;
mMinus = "-";
}
///
<summary>
///
Clean up any resources being used.
///
</summary>
protected
override
void
Dispose( bool
disposing )
{
if(
disposing )
{
if(components
!= null)
{
components.Dispose();
}
}
base.Dispose(
disposing );
}
Windows Form Designer
generated code |
private void
calculator_Load(object
sender, System.EventArgs e)
{
this.Width =
this.CalcCan.Left + this.CalcCan.Width + this.Calc6.Left
* 3 / 2;
this.Btndetail.Text
= "detail";
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);
}
private void
calculator_Activated(object
sender, System.EventArgs e)
{
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
this.ListBox1.Items.Clear();
FirstFlag =
true;
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
}
private void
Calc0_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcNum_Click(object
sender, System.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, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc2_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc3_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc4_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc5_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc6_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc7_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc8_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
Calc9_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcDec_Click(object
sender, System.EventArgs e)
{
this.CalcNum_Click(sender,
e);
}
private void
CalcPlus_Click(object
sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
//Handles +, -,
*, /, =
private void
CalcRes_Click(object
sender, System.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:
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_VCnet.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, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
private void
CalcDiv_Click(object
sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
private void
CalcSub_Click(object
sender, System.EventArgs e)
{
this.CalcRes_Click(sender,
e);
}
//+/- click
event.
private void
CalcSign_Click(object
sender, System.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, System.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, System.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
CalcBS_Click(object
sender, System.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, System.EventArgs e)
{
if (this.Btndetail.Text
== "detail")
{
this.Width =
this.CalcCan.Left + this.CalcCan.Width + (this.Calc6.Left
* 5 / 2) + this.ListBox1.Width;
this.Btndetail.Text
= "hide";
}
else if (this.Btndetail.Text
== "hide")
{
this.Width =
this.CalcCan.Left + this.CalcCan.Width + this.Calc6.Left
* 3 / 2;
this.Btndetail.Text
= "detail";
}
}
}
}
| | |
|
|