|
The
Form design, calculator.vb
Create Visual Basic 2010 Forn -
calculator.vb
Classes used in this Form: ListBox, TextBox,
Button
-
Create the Form
calculator.vb
Add New Item -
calculator.vb Form to work_VBNet_10
Project
- On the Projet
menu , click Add New Item ...,
Add New Item - work_VBNet_10 dialog box appears, Select
Windows form Icon,
in the Name box type
calculator.vb 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.vb created, appears the empty window Form
design |
|
|
The codes file, created
... |
Public
Class
calculator
Private
Sub
calculator_Load(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
MyBase.Load
End
Sub
End
Class
|
|
|
|
|
|
1. |
The
calculator.vb
design |
|
 |
The Properties of
the Form -
calculator.vb... |
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.vb file, the codes after modification
...
|
|
Public
Class
calculator
Inherits
System.Windows.Forms.Form
'Data variables.
Dim mOp1, mOp2
As
Double
'Previously
input operand.
Dim mNumOps
As
Integer
'Number of
operands.
Dim mLastInput
As
Operation
'Indicate
type of last keypress event.
Dim mOpFlag
As
String
'Indicate
pending operation.
Dim mOpPrev
As
String
'Previous
operation
Dim mMinus
As
String
'Minus
operator "-"
Dim mDecValue
As
String
'Local
systems decimal value
Dim mAllowBackSpace
As
Boolean
'Allow
backspace
Dim FirstFlag
As
Boolean
Private
Enum
Operation
None = 0
Operand
[Operator]
CE
Cancel
End
Enum
Private
Sub
calculator_Load(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
MyBase.Load
Me.Width =
Me.ListBox1.Left
+ Me.Calc6.Left
* 1 / 2
Me.Btndetail.Text =
"detail"
Me.Btndetail.Left =
Me.CalcCan.Left
'(Me.CalcMul.Left
+ Me.CalcMul.Width / 2)Class
Me.Btndetail.Top =
Me.CalcField.Top
+ Me.CalcField.Height
- Me.Btndetail.Height
Me.ListBox1.Top =
Me.CalcField.Top
+ (((Me.CalcBS.Top
+ Me.CalcBS.Height)
- Me.CalcField.Top)
/ 2) - (Me.ListBox1.Height
/ 2)
Me.ListBox1.Left =
Me.CalcCan.Left
+ Me.CalcCan.Width
+ (Me.Calc6.Left
* 5 / 4)
End
Sub
Private
Sub
calculator_Activated(ByVal
sender
As
Object,
ByVal
e As
System.EventArgs)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Me.ListBox1.Items.Clear()
FirstFlag =
True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End
Sub
Private
Sub
Calc0_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc0.Click
Me.CalcNum_Click(sender,
e)
End
Sub
'0 - 9, ., click event.
Private
Sub
CalcNum_Click(ByVal
sender
As
Object,
ByVal
e As
System.EventArgs)
mAllowBackSpace =
True
'now
allow backspace
If (mLastInput <>
Operation.Operand)
Then
Me.CalcField.Text =
"0"
End
If
Dim SelButton
As
Button
SelButton =
CType(sender,
Button)
FormatEditField(SelButton.Text)
mLastInput =
Operation.Operand
End
Sub
'format the entry in the text box!
Private
Sub
FormatEditField(ByVal
NewChar
As
String)
Dim Value
As
String
Value =
Me.CalcField.Text
'Determine if there are more than one .'s
If (NewChar =
mDecValue)
Then
'If it's found.
If (InStr(Value,
mDecValue) > 0)
Then
'Don't do anything.
Return
End
If
Value = Value + mDecValue
Else
If Value =
"0"
Then
Value = NewChar
Else
Value = Value + NewChar
End
If
End
If
Me.CalcField.Text =
Value
End
Sub
Private
Sub
Calc1_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc1.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc2_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc2.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc3_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc3.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc4_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc4.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc5_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc5.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc6_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc6.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc7_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc7.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc8_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc8.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
Calc9_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles Calc9.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
CalcDec_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcDec.Click
Me.CalcNum_Click(sender,
e)
End
Sub
Private
Sub
CalcPlus_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcPlus.Click
Me.CalcRes_Click(sender,
e)
End
Sub
'Handles +, -, *, /, =
Private
Sub
CalcRes_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcRes.Click
Dim vline
As
String =
"-"
Dim i
As
Integer
'Now do not allow backspace(CalcBS) actions after the
result has been
'calculated and displayed.
mAllowBackSpace =
False
If (mLastInput.Equals(Operation.Operand))
Then
mNumOps = mNumOps + 1
End
If
Select
Case
mNumOps
Case 1
'
ListBox1 is Empty
If
ListBox1.Items.Count = 0
Then
Me.ListBox1.Items.Add(CalcField.Text)
mOp1 =
Double.Parse(CalcField.Text)
Case 2
mOp2 =
Double.Parse(CalcField.Text)
Select
Case
mOpFlag
Case
"+"
mOp1 = mOp1 + mOp2
Case
"-"
mOp1 = mOp1 - mOp2
Case
"*"
mOp1 = mOp1 * mOp2
Case
"/"
If (mOp2 = 0)
Then
MsgBox( "Can't
divide by zero! ...",
MsgBoxStyle.DefaultButton1
Or
MsgBoxStyle.Information
Or
MsgBoxStyle.OkOnly,
Title)
Else
mOp1 = mOp1 / mOp2
End
If
Case
"="
mOp1 = mOp2
End
Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If (mOpFlag =
"=")
Then
Else
Me.ListBox1.Items.Add(mOpFlag)
If (mNumOps = 2)
Then
Me.ListBox1.Items.Add("
" & mOp2.ToString)
End
If
End
If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Me.CalcField.Text =
mOp1.ToString()
mNumOps = 1
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If (mOpFlag =
"=")
Then
Me.ListBox1.Items.Add("
" &
"..........
new operation ..........")
Me.ListBox1.Items.Add("
" & mOp1.ToString)
Me.ListBox1.SelectedIndex
= ListBox1.Items.Count - 1
Else
For i = 0
To
(Len(mOp1.ToString) * 2) - 4
vline = vline +
"-"
Next
Me.ListBox1.Items.Add("
" & vline)
Me.ListBox1.Items.Add("
" & mOp1.ToString)
Me.ListBox1.SelectedIndex
= ListBox1.Items.Count - 1
End
If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End
Select
mLastInput =
Operation.Operator
mOpPrev = mOpFlag
Dim ButtonPressed
As
Button
ButtonPressed =
CType(sender,
Button)
mOpFlag = ButtonPressed.Text
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If FirstFlag
Then
Me.ListBox1.Items.Add("
" &
"..........
new operation ..........")
If (mNumOps = 1)
Then
Me.ListBox1.Items.Add("
" & mOp1)
End
If
FirstFlag =
False
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End
Sub
Private
Sub
CalcSub_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcSub.Click
Me.CalcRes_Click(sender,
e)
End
Sub
Private
Sub
CalcMul_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcMul.Click
Me.CalcRes_Click(sender,
e)
End
Sub
Private
Sub
CalcDiv_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcDiv.Click
Me.CalcRes_Click(sender,
e)
End
Sub
'+/- click event.
Private
Sub
CalcSign_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcSign.Click
If (mLastInput <>
Operation.Operand)
Then
Me.CalcField.Text =
"0"
Else
If (Mid(Me.CalcField.Text,
1, 1)) = mMinus
Then
Me.CalcField.Text =
Mid(Me.CalcField.Text,
2)
Else
If
Me.CalcField.Text
<>
"0"
Then
Me.CalcField.Text =
mMinus &
Me.CalcField.Text
End
If
End
If
End
If
mLastInput =
Operation.Operand
End
Sub
'Cancel click event.
Private
Sub
CalcCan_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
CalcCan.Click
Reset()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Me.ListBox1.Items.Clear()
FirstFlag =
True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End
Sub
'
Helper to initialize the vals.
Private
Sub
Reset()
mOp1 = 0
mOp2 = 0
mNumOps = 0
mLastInput =
Operation.None
mOpFlag =
""
mOpPrev =
""
Me.CalcField.Text =
"0"
mAllowBackSpace =
True
End
Sub
'Cancel entry click event.
Private
Sub
CalcCE_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles CalcCE.Click
If (mLastInput.Equals(Operation.Operand))
Then
Me.CalcField.Text =
"0"
ElseIf (mLastInput.Equals(Operation.[Operator]))
Then
mOpFlag = mOpPrev
End
If
mLastInput =
Operation.CE
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Me.ListBox1.Items.Clear()
FirstFlag =
True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End
Sub
'BS click event.
Private
Sub
CalcBS_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles CalcBS.Click
Dim Value
As
String
'Check if we can backspace before removing items from
CalcField.text.
If mAllowBackSpace
Then
Value =
Me.CalcField.Text
If Len(Value) > 1
Then
Value = Mid(Value, 1, Len(Value) - 1)
Else
Value =
"0"
End
If
'
If all we are left is the minus sign, reset.
If Value =
"-"
Then
Value =
"0"
End
If
Me.CalcField.Text =
Value
End
If
End
Sub
Private
Sub
Btndetail_Click(ByVal
sender
As
System.Object,
ByVal e
As
System.EventArgs)
Handles
Btndetail.Click
If
Me.Btndetail.Text
=
"detail"
Then
Me.Width =
Me.ListBox1.Left
+ Me.ListBox1.Width
+ Me.Calc6.Left
* 2
Me.Btndetail.Text =
"hide"
Exit Sub
End
If
If
Me.Btndetail.Text
=
"hide"
Then
Me.Width =
Me.ListBox1.Left
+ Me.Calc6.Left
* 1 / 2
Me.Btndetail.Text =
"detail"
End
If
End
Sub
End
Class
| | |
|
|