VB如何制作计算器

VB如何制作计算器 详细代码

Dim Num1, Num2Dim SPrivate Sub Command2_Click() If InStr(1, Text1.Text, ".") = 0 Then Text1.Text = Text1.Text & "." End If sFocusEnd SubPrivate Sub Command3_Click() Text1.Text = IIf(InStr(1, Text1.Text, ".") <> 0, Format(Num2, "#0.#"), Num2) Num1 = Val(Text1.Text)End SubPrivate Sub Command4_Click() Text1.Text = "0" sFocus S = 1End SubPrivate Sub Command5_Click() Text1.Text = "0" sFocus S = 2End SubPrivate Sub Command6_Click() Text1.Text = "0" sFocus S = 3End SubPrivate Sub Command7_Click() Text1.Text = "0" sFocus S = 4End SubPrivate Sub Command8_Click() Text1.Text = "0" Num1 = 0 S = 0 sFocusEnd SubPrivate Sub Form_GotFocus() sFocusEnd SubPrivate 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 sFocusEnd SubPrivate Sub sFocus() Text1.SetFocus Text1.SelStart = Len(Text1.Text)End SubPrivate Sub Form_Load() Text1.Locked = True Text1.SelStart = Len(Text1.Text)End SubPrivate 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 SelectEnd Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-05-02
VB可以做一个简单的计算器,界面想怎么美观都可以,
----------------------------------------------------
然后复制以下代码:
Private Sub Form_Load()
t1 = ""
Text2 = ""
t2 = ""
End Sub
Private Sub Command1_Click()
If Text2.Text = "" Then
t1.Text = t1.Text + "1"
Else
t2.Text = t2.Text + "1"
End If End Sub
Private Sub Command16_Click()
Select Case Text2
Case "+"
result = Val(t1.Text) + Val(t2.Text)
Case "-"
result = Val(t1.Text) - Val(t2.Text)
Case "*"
result = Val(t1.Text) * Val(t2.Text)
Case "/"
result = Val(t1.Text) / Val(t2.Text)
End Select
Text2 = ""
t2 = ""
Text1.Text = result
t1.Text = Text1.Text
End Sub