谢谢
http://post.baidu.com/f?kz=209715785我的计算器Option Explicit Dim FocusText As VB.TextBox Dim pd As Boolean Dim x As Double Dim y As Double Dim ch As Integer Private Sub Command1_Click(Index As Integer) On Error Resume Next FocusText.SetFocus If Index SendKeys Index ElseIf Index = 10 Then ch = Index - 10 x = Val(Text1.Text) Text1.Text = "" ElseIf Index = 14 Then y = Val(Text1.Text) Select Case ch Case 0 Text1.Text = x + y Case 1 Text1.Text = x - y Case 2 Text1.Text = x * y Case 3 Text1.Text = x / y End Select ElseIf Index = 15 Then FocusText.SetFocus SendKeys "{BS}" ElseIf Index = 16 Then FocusText.SetFocus SendKeys "." End If End Sub '防止输入多个小数点以及出数字外的其他字符 Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii 57) And KeyAscii 46 And KeyAscii 8 Then KeyAscii = 0 End If If pd = True And KeyAscii = 46 Then KeyAscii = 0 End If If pd = False And KeyAscii = 46 Then pd = True KeyAscii = 46 End If End Sub '防止 Ctrl + v 粘贴 Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyV And Shift = vbCtrlMask Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub '防止右键粘贴 Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbRightButton Then Text1.Enabled = False Clipboard.Clear Text1.Enabled = True End If End Sub Private Sub Text1_LostFocus() Set FocusText = Text1 End Sub 界面如下 0为Command1(0)1为Command1(1)依次类推 ←为Command1(15) 小数点为Command1(16) 贴子相关图片: 界面可以到
温馨提示:答案为网友推荐,仅供参考
第1个回答 2007-05-29
试试这个~
Private Sub Command1_Click()
Label1 = Val(Text1) + Val(Text2)
End Sub
Private Sub Command2_Click()
Label1 = Val(Text1) - Val(Text2)
End Sub
Private Sub Command3_Click()
Label1 = Val(Text1) * Val(Text2)
End Sub
Private Sub Command4_Click()
If Val(Text2) <> 0 Then
Label1 = Val(Text1) / Val(Text2)
Else
MsgBox "除数不能为0"
Text2 = ""
Text2.SetFocus
End If
End Sub本回答被网友采纳
Private Sub Command1_Click()
Label1 = Val(Text1) + Val(Text2)
End Sub
Private Sub Command2_Click()
Label1 = Val(Text1) - Val(Text2)
End Sub
Private Sub Command3_Click()
Label1 = Val(Text1) * Val(Text2)
End Sub
Private Sub Command4_Click()
If Val(Text2) <> 0 Then
Label1 = Val(Text1) / Val(Text2)
Else
MsgBox "除数不能为0"
Text2 = ""
Text2.SetFocus
End If
End Sub本回答被网友采纳
第2个回答 2019-10-17
很简单的:窗口放入四个COMMAND控件(Caption属性分别为+、-、X、÷),三个TEXT控件,再放入三个Label控件(Caption属性分别为:操作数1、操作数2和计算结果),窗口的Caption属性改为“四则运算”
代码如下:
Private
Sub
Command1_Click()
If
Val(Text1.Text)
+
Val(Text2.Text)
<>
Int(Val(Text1.Text)
+
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
+
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
+
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command2_Click()
If
Val(Text1.Text)
-
Val(Text2.Text)
<>
Int(Val(Text1.Text)
-
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
-
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
-
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command3_Click()
If
Val(Text1.Text)
*
Val(Text2.Text)
<>
Int(Val(Text1.Text)
*
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
*
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
*
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command4_Click()
If
Val(Text1.Text)
/
Val(Text2.Text)
<>
Int(Val(Text1.Text)
/
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
/
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
/
Val(Text2.Text)
End
If
End
Sub
代码如下:
Private
Sub
Command1_Click()
If
Val(Text1.Text)
+
Val(Text2.Text)
<>
Int(Val(Text1.Text)
+
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
+
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
+
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command2_Click()
If
Val(Text1.Text)
-
Val(Text2.Text)
<>
Int(Val(Text1.Text)
-
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
-
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
-
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command3_Click()
If
Val(Text1.Text)
*
Val(Text2.Text)
<>
Int(Val(Text1.Text)
*
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
*
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
*
Val(Text2.Text)
End
If
End
Sub
Private
Sub
Command4_Click()
If
Val(Text1.Text)
/
Val(Text2.Text)
<>
Int(Val(Text1.Text)
/
Val(Text2.Text))
Then
Text3.Text
=
Format(Val(Text1.Text)
/
Val(Text2.Text),
"0.######")
Else
Text3.Text
=
Val(Text1.Text)
/
Val(Text2.Text)
End
If
End
Sub