如何用VB6.0制作计算器???

2024-12-31 04:37:23
推荐回答(2个)
回答1:

Dim Num1, Num2
Dim S

Private Sub Command2_Click()
If InStr(1, Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
End If
sFocus
End Sub

Private Sub Command3_Click()
Text1.Text = IIf(InStr(1, Text1.Text, ".") <> 0, Format(Num2, "#0.#"), Num2)
Num1 = Val(Text1.Text)
End Sub

Private Sub Command4_Click()
Text1.Text = "0"
sFocus
S = 1
End Sub

Private Sub Command5_Click()
Text1.Text = "0"
sFocus
S = 2
End Sub

Private Sub Command6_Click()
Text1.Text = "0"
sFocus
S = 3
End Sub

Private Sub Command7_Click()
Text1.Text = "0"
sFocus
S = 4
End Sub

Private Sub Command8_Click()
Text1.Text = "0"
Num1 = 0
S = 0
sFocus
End Sub

Private Sub Form_GotFocus()
sFocus
End Sub

Private Sub Command1_Click(Index As Integer)
Text1.Text = Val(Text1.Text & Index)
Select Case S
Case 0
Num1 = Val(Text1.Text)
Case 1
Num2 = Num1 + Val(Text1.Text)
Case 2
Num2 = Num1 - Val(Text1.Text)
Case 3
Num2 = Num1 * Val(Text1.Text)
Case 4
Num2 = Num1 / Val(Text1.Text)
End Select
sFocus
End Sub

Private Sub sFocus()
Text1.SetFocus
Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Form_Load()
Text1.Locked = True
Text1.SelStart = Len(Text1.Text)
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 96 To 105 '(0-9)小键盘
Command1_Click (KeyCode - 96)
Case 48 To 57 '(0-9)大键盘
Command1_Click (KeyCode - 48)
Case 110, 190 '(.)键盘上的两个点
Command2_Click
Case 107 '(+)小键盘加号
Command4_Click
Case 109, 189 '(-)小键盘减号和大键盘短横
Command5_Click
Case 106 '(*)小键盘乘号
Command6_Click
Case 111, 191, 220 '(/,\)小键盘除号和大键盘两个斜杠
Command7_Click
Case 13, 187 '(=)回车键和大键盘等于号
Command3_Click
Case 46 '(Del)键盘删除键
Command8_Click
End Select
End Sub

回答2:

Private Sub Command1_Click(Index As Integer) '0-9数字键
Text1.Text = Text1.Text & Index
Text1.SetFocus
End Sub

Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer) '阻止键盘输入,不过不知为什么没有用
KeyAscii = 0
End Sub

Private Sub Command2_Click(Index As Integer) ' "."键
Text1.Text = Text1.Text & "."
End Sub

Private Sub Command3_Click() '退格键
If Text1.Text = "" Then
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
End Sub

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

Private Sub Command5_Click(Index As Integer) '做运算,总觉得逻辑上哪有问题
shu1 = Val(Text1.Text)
process = shu1
Text1.Text = ""
If judge = Command5(0) Then
process0 = shu1 + Command(0)
ElseIf judge = Command5(1) Then
process1 = shu1 + Command5(1)
ElseIf judge = Command5(2) Then
process2 = shu1 + Command(2)
ElseIf judge = Command5(3) Then
process3 = shu1 + Command5(3)
End If
shu2 = Val(Text1.Text)
End Sub

Private Sub Command6_Click() '等号
result = process + shu2
Text1.Text = result
If process0 Then
result = shu1 + shu2
ElseIf process1 Then
result = shu1 - shu2
ElseIf process2 Then
result = shu1 * shu2
ElseIf process3 Then
result = shu1 / shu2
End If
End Sub

Private Sub Text1_Change()
If judge = False Then
shu2 = Val(Text1.Text)
End If
Text1.SetFocus
End Sub