Develop an application to design a simple calculator that performs the operations +,-,*,/, % , and Modulo operation.
To get an idea , Click Simple Programs(3)
Refer The four calculations , Calculations using Grid control and Basic Mathematical functions also.
(A)Program Design :
(B) Property(Controls Used) :
- One TextBox (Name-Text1) to input values as well as calculated output
- 4 Command Buttons with Captions as .(Name-cmd_dot) , +/-(Name-cmd_sign) , =(Name-com_equal) , C(Name-com_cancel)
- 10 Command Buttons with Name-Command1, Captions from 0 to 9 , Index from 0 yo 9
- 6 Command Buttons with Index from 0 to 5 , Text Property as /, * ,- , + , mod , % respectively and Name as com_op
Note that , there is only one Text Box and 20 Command Buttons.
(C) Attaching Code to the Object :
Dim old As String Dim old2 As String Dim op As String Dim op2 As String Dim f As Integer |
If op2 = "%" Then old_value = Val(old) Text1.Text = Val(old_value * (Val(Text1.Text) / 100)) f = 1 End If End Sub |
Private Sub calc_text() Dim ans As Double Dim old_val As Double old_value = Val(old) Select Case op Case "+" ans = old_value + Val(old2) Case "-" ans = old_value - Val(old2) Case "*" ans = old_value * Val(old2) Case "/" Case "mod" ans = old_value - (CInt(old_value / Val(old2)) * Val(old2)) End Select Text1 = Str(ans) old = Text1 End Sub |
Private Sub cmd_dot_Click() Text1 = Text1 & "." End Sub |
Private Sub cmd_sign_Click() Text1.Text = -1 * Val(Text1.Text) End Sub |
Private Sub com_cancel_Click() Text1.Text = "" old = "" op = "" op2 = "" End Sub |
Private Sub com_equal_Click() If op2 = "=" Then Else old2 = Text1.Text End If calc_text f = 1 op2 = "=" End Sub |
Private Sub cmd_op_Click(Index As Integer) If com_op(Index).Caption = "%" Then op2 = "%" calc_per Else If op <> "" And op2 <> "=" Then old2 = Text1.Text calc_text End If old = Text1.Text op = com_op(Index).Caption f = 1 End If op2 = "" End Sub |
Private Sub Command1_Click(Index As Integer) If f = 1 Then Text1 = Command1(Index).Caption Else Text1 = Text1 & Command1(Index).Caption End If f = 0 End Sub |
Private Sub Form_KeyPress(KeyAscii As Integer) MsgBox KeyAscii End Sub Private Sub Form_Load() old = "" old2 = "" op = "" f = 0 End Sub |
Private Sub Text1_KeyPress(KeyAscii As Integer) If ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = 46 Or KeyAscii = 27) Then Else KeyAscii = 8 End If End Sub |
- ====================================================================