亲各位高手帮忙啊!我要用vb编写个按钮的计算器我吧图发过来请各位高手帮帮忙!急急急!

2024-12-27 06:47:54
推荐回答(3个)
回答1:

Dim a As Long

Private Sub Command1_Click()
Text1.Text = Text1.Text + "1"
End Sub

Private Sub Command10_Click()
a = Text1.Text
Text1.Text = ""
Dim c As Long
c = 0
End Sub

Private Sub Command11_Click()
a = Text1.Text
Text1.Text = ""
Dim c As Long
c = 1
End Sub

Private Sub Command12_Click()
a = Text1.Text
Text1.Text = ""
Dim c As Long
c = 2
End Sub

Private Sub Command13_Click()
a = Text1.Text
Text1.Text = ""
Dim c As String
c = 3
End Sub

Private Sub Command14_Click()
Select Case c
Case c = 0
Text1.Text = a + Text1.Text
Case c = 1
Text1.Text = a - Text1.Text
Case c = 2
Text1.Text = a * Text1.Text
Case c = 3
Text1.Text = a / Text1.Text
End Select
End Sub

Private Sub Command17_Click()
Text1.Text = 0
End Sub

Private Sub Command2_Click()
Text1.Text = Text1.Text & 2
End Sub

Private Sub Command3_Click()
Text1.Text = Text1.Text + "3"
End Sub

Private Sub Command4_Click()
Text1.Text = Text1.Text + "4"
End Sub

Private Sub Command5_Click()
Text1.Text = Text1.Text + "5"
End Sub

Private Sub Command6_Click()
Text1.Text = Text1.Text + "6"
End Sub

Private Sub Command7_Click()
Text1.Text = Text1.Text + "7"
End Sub

Private Sub Command8_Click()
Text1.Text = Text1.Text + "8"
End Sub

Private Sub Command9_Click()
Text1.Text = Text1.Text + "9"
End Sub

回答2:

Begin VB.Form Calculator
BorderStyle = 1 'Fixed Single
Caption = "计算器"
ClientHeight = 2970
ClientLeft = 2580
ClientTop = 1485
ClientWidth = 3270
ClipControls = 0 'False
BeginProperty Font
Name = "宋体"
Size = 10
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty

LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
PaletteMode = 1 'UseZOrder
ScaleHeight = 2970
ScaleWidth = 3270
WhatsThisHelp = -1 'True
Begin VB.CommandButton Number
Caption = "7"
Height = 480
Index = 7
Left = 120
TabIndex = 7
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "8"
Height = 480
Index = 8
Left = 720
TabIndex = 8
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "9"
Height = 480
Index = 9
Left = 1320
TabIndex = 9
Top = 600
Width = 480
End
Begin VB.CommandButton Cancel
Caption = "C"
Height = 480
Left = 2040
TabIndex = 10
Top = 600
Width = 480
End
Begin VB.CommandButton CancelEntry
Caption = "CE"
Height = 480
Left = 2640
TabIndex = 11
Top = 600
Width = 480
End
Begin VB.CommandButton Number
Caption = "4"
Height = 480
Index = 4
Left = 120
TabIndex = 4
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "5"
Height = 480
Index = 5
Left = 720
TabIndex = 5
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "6"
Height = 480
Index = 6
Left = 1320
TabIndex = 6
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "+"
Height = 480
Index = 1
Left = 2040
TabIndex = 12
Top = 1200
Width = 480
End
Begin VB.CommandButton Operator
Caption = "-"
Height = 480
Index = 3
Left = 2640
TabIndex = 13
Top = 1200
Width = 480
End
Begin VB.CommandButton Number
Caption = "1"
Height = 480
Index = 1
Left = 120
TabIndex = 1
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "2"
Height = 480
Index = 2
Left = 720
TabIndex = 2
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "3"
Height = 480
Index = 3
Left = 1320
TabIndex = 3
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "X"
Height = 480
Index = 2
Left = 2040
TabIndex = 14
Top = 1800
Width = 480
End
Begin VB.CommandButton Operator
Caption = "/"
Height = 480
Index = 0
Left = 2640
TabIndex = 15
Top = 1800
Width = 480
End
Begin VB.CommandButton Number
Caption = "0"
Height = 480
Index = 0
Left = 120
TabIndex = 0
Top = 2400
Width = 1080
End
Begin VB.CommandButton Decimal
Caption = "."
Height = 480
Left = 1320
TabIndex = 18
Top = 2400
Width = 480
End
Begin VB.CommandButton Operator
Caption = "="
Height = 480
Index = 4
Left = 2040
TabIndex = 16
Top = 2400
Width = 480
End
Begin VB.CommandButton Percent
Caption = "%"
Height = 480
Left = 2640
TabIndex = 17
Top = 2400
Width = 480
End
Begin VB.Label Readout
Alignment = 1 'Right Justify
BackColor = &H0000FFFF&
BorderStyle = 1 'Fixed Single
Caption = "0."
BeginProperty Font
Name = "宋体"
Size = 12
Charset = 0
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00000000&
Height = 375
Left = 120
TabIndex = 19
Top = 105
Width = 3000
End
End
Attribute VB_Name = "Calculator"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' ------------------------------------------------------------------------
' 版权所有 (C) 1994 Microsoft Corporation
'
' 您可以免费以任何方式使用、修改、复制并分发您认为有用的
' 示例应用程序文件 (或任何修改过的版本)。Microsoft 对任何
' 示例应用程序文件不做任何保证,不负任何责任和义务。
' ------------------------------------------------------------------------
Option Explicit
Dim Op1, Op2 ' 预先输入操作数。
Dim DecimalFlag As Integer ' 小数点存在吗?
Dim NumOps As Integer ' 操作数个数。
Dim LastInput ' 指示上一次按键事件的类型。
Dim OpFlag ' 指示未完成的操作。
Dim TempReadout

' C (取消) 按钮的 Click 事件过程
' 重新设置显示并初始化变量。
Private Sub Cancel_Click()
Readout = Format(0, "0.")
Op1 = 0
Op2 = 0
Form_Load
End Sub

' CE (取消输入) 按钮的 Click 事件过程。
Private Sub CancelEntry_Click()
Readout = Format(0, "0.")
DecimalFlag = False
LastInput = "CE"
End Sub

' 小数点 (.) 按钮的 Click 事件过程
' 如果上一次按键为运算符,初始化 readout 为 "0.";
' 否则显示时追加一个小数点。
Private Sub Decimal_Click()
If LastInput = "NEG" Then
Readout = Format(0, "-0.")
ElseIf LastInput <> "NUMS" Then
Readout = Format(0, "0.")
End If
DecimalFlag = True
LastInput = "NUMS"
End Sub

' 窗体的初始化过程
' 设置所有变量为其初始值。
Private Sub Form_Load()
DecimalFlag = False
NumOps = 0
LastInput = "NONE"
OpFlag = " "
Readout = Format(0, "0.")
'Decimal.Caption = Format(0, ".")
End Sub

' 数字键 (0-9) 的 Click 事件过程
' 向显示中的数追加新数。
Private Sub Number_Click(Index As Integer)
If LastInput <> "NUMS" Then
Readout = Format(0, ".")
DecimalFlag = False
End If
If DecimalFlag Then
Readout = Readout + Number(Index).Caption
Else
Readout = Left(Readout, InStr(Readout, Format(0, ".")) - 1) + Number(Index).Caption + Format(0, ".")
End If
If LastInput = "NEG" Then Readout = "-" & Readout
LastInput = "NUMS"
End Sub

' 运算符 (+, -, x, /, =) 的 Click 事件过程
' 如果接下来的按键是数字键,增加 NumOps。
' 如果有一个操作数,则设置 Op1。
' 如果有两个操作数,则将 Op1 设置为 Op1 与
' 当前输入字符串的运算结果,并显示结果。
Private Sub Operator_Click(Index As Integer)
TempReadout = Readout
If LastInput = "NUMS" Then
NumOps = NumOps + 1
End If
Select Case NumOps
Case 0
If Operator(Index).Caption = "-" And LastInput <> "NEG" Then
Readout = "-" & Readout
LastInput = "NEG"
End If
Case 1
Op1 = Readout
If Operator(Index).Caption = "-" And LastInput <> "NUMS" And OpFlag <> "=" Then
Readout = "-"
LastInput = "NEG"
End If
Case 2
Op2 = TempReadout
Select Case OpFlag
Case "+"
Op1 = CDbl(Op1) + CDbl(Op2)
Case "-"
Op1 = CDbl(Op1) - CDbl(Op2)
Case "X"
Op1 = CDbl(Op1) * CDbl(Op2)
Case "/"
If Op2 = 0 Then
MsgBox "除数不能为零", 48, "计算器"
Else
Op1 = CDbl(Op1) / CDbl(Op2)
End If
Case "="
Op1 = CDbl(Op2)
Case "%"
Op1 = CDbl(Op1) * CDbl(Op2)
End Select
Readout = Op1
NumOps = 1
End Select
If LastInput <> "NEG" Then
LastInput = "OPS"
OpFlag = Operator(Index).Caption
End If
End Sub

' 百分比键 (%) 的 Click 事件过程
' 计算并显示第一个操作数的百分数。
Private Sub Percent_Click()
Readout = Readout / 100
LastInput = "Ops"
OpFlag = "%"
NumOps = NumOps + 1
DecimalFlag = True
End Sub

回答3:

VERSION 5.00
Begin VB.Form Form1
Caption = "个人计算器"
ClientHeight = 7320
ClientLeft = 60
ClientTop = 450
ClientWidth = 11070
LinkTopic = "Form1"
Picture = "Form3.frx":0000
ScaleHeight = 7320
ScaleWidth = 11070
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command17
Caption = "0"
Height = 615
Left = 4680
TabIndex = 19
Top = 5520
Width = 1455
End
Begin VB.PictureBox Picture2
Height = 4815
Left = 9000
Picture = "Form3.frx":30827
ScaleHeight = 4755
ScaleWidth = 1875
TabIndex = 18
Top = 600
Width = 1935
End
Begin VB.PictureBox Picture1
Height = 4800
Left = 120
Picture = "Form3.frx":4E1A2
ScaleHeight = 4740
ScaleWidth = 1875
TabIndex = 17
Top = 600
Width = 1935
End
Begin VB.CommandButton Command16
Caption = "="
Height = 615
Left = 7680
TabIndex = 16
Top = 4800
Width = 1095
End
Begin VB.CommandButton Command15
Caption = "清除"
Height = 615
Left = 7680
TabIndex = 15
Top = 3720
Width = 1095
End
Begin VB.CommandButton Command14
Caption = "^"
Height = 615
Left = 7680
TabIndex = 14
Top = 2640
Width = 1095
End
Begin VB.CommandButton Command13
Caption = "/"
Height = 615
Left = 7680
TabIndex = 13
Top = 1560
Width = 1095
End
Begin VB.CommandButton Command12
Caption = "9"
Height = 615
Left = 5760
TabIndex = 12
Top = 4800
Width = 1095
End
Begin VB.CommandButton Command11
Caption = "8"
Height = 615
Left = 3960
TabIndex = 11
Top = 4800
Width = 1095
End
Begin VB.CommandButton Command10
Caption = "7"
Height = 615
Left = 2160
TabIndex = 10
Top = 4800
Width = 1095
End
Begin VB.CommandButton Command9
Caption = "6"
Height = 615
Left = 5760
TabIndex = 9
Top = 3720
Width = 1095
End
Begin VB.CommandButton Command8
Caption = "5"
Height = 615
Left = 3960
TabIndex = 8
Top = 3720
Width = 1095
End
Begin VB.CommandButton Command7
Caption = "4"
Height = 615
Left = 2160
TabIndex = 7
Top = 3720
Width = 1095
End
Begin VB.CommandButton Command6
Caption = "3"
Height = 615
Left = 5760
TabIndex = 6
Top = 2640
Width = 1095
End
Begin VB.CommandButton Command5
Caption = "2"
Height = 615
Left = 3960
TabIndex = 5
Top = 2640
Width = 1095
End
Begin VB.CommandButton Command4
Caption = "1"
Height = 615
Left = 2160
TabIndex = 4
Top = 2640
Width = 1095
End
Begin VB.CommandButton Command3
Caption = "*"
Height = 615
Left = 5760
TabIndex = 3
Top = 1560
Width = 1095
End
Begin VB.CommandButton Command2
Caption = "-"
Height = 615
Left = 3960
TabIndex = 2
Top = 1560
Width = 1095
End
Begin VB.CommandButton Command1
Caption = "+"
Height = 615
Left = 2160
TabIndex = 1
Top = 1560
Width = 1095
End
Begin VB.TextBox Text1
Alignment = 1 'Right Justify
Height = 615
Left = 2160
TabIndex = 0
Text = "Text1"
Top = 600
Width = 6735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim b As Double '定义一个代表在文本框中输入的数
Dim a As Double '定义一个代表符号的量

Private Sub Command1_Click()
a = 1 '当是加法时
b = Text1.Text '将加法指令输入文本框
Text1.Text = "" '文本框中不显现加法符号
End Sub

Private Sub Command10_Click()
Text1.Text = (Text1.Text) & "7" '按键7 显示在文本框中
End Sub

Private Sub Command11_Click()
Text1.Text = (Text1.Text) & "8" '按键8显示在文本框中
End Sub

Private Sub Command12_Click()
Text1.Text = (Text1.Text) & "9" '按键9显示在文本框中
End Sub

Private Sub Command13_Click()
a = 4 '当是除法时
b = Text1.Text '将除法指令输入文本框中
Text1.Text = "" '除法符号不显示在文本框中
End Sub

Private Sub Command14_Click()
a = 5 '当是乘方时
b = Text1.Text '将乘方指令输入文本框中
Text1.Text = "" '乘方符号不显示在文本框中
End Sub

Private Sub Command15_Click()
Text1.Text = "" '清除键
End Sub

Private Sub Command16_Click()
Text1.FontName = "华文行楷"
Text1.FontBold = True
Text1.BackColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
Text1.FontSize = 25
Text1.ForeColor = RGB(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))

If (a = 1) Then '当是加法时,将输入的数显示在文本框中
Text1.Text = b + Text1.Text
End If
If (a = 2) Then '当是减法时,将输入的数显示在文本框中
Text1.Text = b - Text1.Text
End If
If (a = 3) Then
Text1.Text = b * Text1.Text '当是乘法时,将输入的数显示在文本框中
End If
If (a = 4) Then
Text1.Text = b / Text1.Text '当是除法时,将输入的数显示在文本框中
End If
If (a = 5) Then
Text1.Text = b ^ Text1.Text '当是乘方时,将输入的数显示在文本框中
End If

End Sub

Private Sub Command17_Click()
Text1.Text = (Text1.Text) & "0" '按键0显示在文本框中
End Sub

Private Sub Command2_Click()
a = 2 '当是减法时
b = Text1.Text
Text1.Text = ""
End Sub

Private Sub Command3_Click() '当是乘法时
a = 3
b = Text1.Text
Text1.Text = ""
End Sub

Private Sub Command4_Click() '按键1显示在文本框中
Text1.Text = (Text1.Text) & "1"
End Sub

Private Sub Command5_Click()
Text1.Text = (Text1.Text) & "2" '按键2显示在文本框中
End Sub

Private Sub Command6_Click()
Text1.Text = (Text1.Text) & "3" '按键3显示在文本框中
End Sub

Private Sub Command7_Click()
Text1.Text = (Text1.Text) & "4" '按键4显示在文本框中
End Sub

Private Sub Command8_Click()
Text1.Text = (Text1.Text) & "5" '按键5显示在文本框中
End Sub

Private Sub Command9_Click()
Text1.Text = (Text1.Text) & "6" '按键6显示在文本框中
End Sub

Private Sub Form_Load()

MsgBox "正在加载"
Text1.Text = ""

End Sub

Private Sub Text1_Change()
'文本框背景颜色
Dim red As Integer
Dim green As Integer
Dim blue As Integer
red = Int(32000 * Rnd) Mod 256
green = Int(32000 * Rnd) Mod 256
blue = Int(32000 * Rnd) Mod 256
Text1.ForeColor = RGB(red, green, blue)
Text1.FontSize = 25
End Sub