Option Explicit Dim Op1, Op2 ' Previously input operand. Dim firstInput As Boolean ' Indicate type of last keypress event. Dim OpFlag, lastinput ' Indicate pending operation. Private Sub Form_Load() Dim i As Integer, NumWidth As Integer NumWidth = (number(0).Width) For i = 1 To 9 Load number(i) If i Mod 3 = 1 Then number(i).Top = number(i - 1).Top - number(i - 1).Height - 100 number(i).Left = number(0).Left Else number(i).Top = number(i - 1).Top number(i).Left = number(i - 1).Left + number(i - 1).Width + 100 End If number(i).Caption = i number(i).Visible = -1 Next i number(0).Width = number(1).Width * 3 + 100 firstInput = True lastinput = "" End SubPrivate Sub Clea_Click()
Disp = Format(0, "0") Op1 = 0 Op2 = 0 firstInput = True lastinput = "" End Sub Private Sub Number_Click(Index As Integer) If lastinput <> "NUMS" Then Disp = number(Index).Caption Else Disp = Disp + number(Index).Caption End If lastinput = "NUMS" End Sub Private Sub Operator_Click(Index As Integer) If firstInput = True Then Op1 = Val(Disp) firstInput = False Else Op2 = Val(Disp) Select Case OpFlag Case "+" Op1 = Op1 + Op2 Case "-" Op1 = Op1 - Op2 Case "℅" Op1 = Op1 * Op2 Case "¯" If Op2 = 0 Then MsgBox "Can't divide by zero", 48, "Calculator" Else Op1 = Op1 / Op2 End If End Select If Operator(Index).Caption = "=" Then Disp = Op1 End If lastinput = "oper" OpFlag = Operator(Index).Caption End SubPrivate Sub tran_Click(Index As Integer) Select Case Index Case 0 Disp = Oct(Val(Disp)) Case 1 Disp = Hex(Val(Disp)) End Select End Sub
A Simple Calculator (Great for beginners to VB) http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=51201&lngWId=1 http://www.pscode.com/Upload_PSC/ScreenShots/PIC2004123231357899.gif
Dim Op1, Op2 ' Previously input operand.
Dim firstInput As Boolean ' Indicate type of last keypress event.
Dim OpFlag, lastinput ' Indicate pending operation.
Private Sub Form_Load() Dim i As Integer, NumWidth As Integer
NumWidth = (number(0).Width)
For i = 1 To 9
Load number(i)
If i Mod 3 = 1 Then
number(i).Top = number(i - 1).Top - number(i - 1).Height - 100
number(i).Left = number(0).Left
Else
number(i).Top = number(i - 1).Top
number(i).Left = number(i - 1).Left + number(i - 1).Width + 100
End If
number(i).Caption = i
number(i).Visible = -1
Next i
number(0).Width = number(1).Width * 3 + 100
firstInput = True
lastinput = ""
End SubPrivate Sub Clea_Click()
Disp = Format(0, "0")
Op1 = 0
Op2 = 0
firstInput = True
lastinput = ""
End Sub
Private Sub Number_Click(Index As Integer)
If lastinput <> "NUMS" Then
Disp = number(Index).Caption
Else
Disp = Disp + number(Index).Caption
End If
lastinput = "NUMS"
End Sub
Private Sub Operator_Click(Index As Integer)
If firstInput = True Then
Op1 = Val(Disp)
firstInput = False
Else
Op2 = Val(Disp)
Select Case OpFlag
Case "+"
Op1 = Op1 + Op2
Case "-"
Op1 = Op1 - Op2
Case "℅"
Op1 = Op1 * Op2
Case "¯"
If Op2 = 0 Then
MsgBox "Can't divide by zero", 48, "Calculator"
Else
Op1 = Op1 / Op2
End If
End Select
If Operator(Index).Caption = "=" Then Disp = Op1
End If
lastinput = "oper"
OpFlag = Operator(Index).Caption
End SubPrivate Sub tran_Click(Index As Integer)
Select Case Index
Case 0
Disp = Oct(Val(Disp))
Case 1
Disp = Hex(Val(Disp))
End Select
End Sub
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=51201&lngWId=1
http://www.pscode.com/Upload_PSC/ScreenShots/PIC2004123231357899.gif
http://www.pscode.com/upload_PSC/screenshots/PIC2002111634472324.jpg