编写计算器的VB代码(有图)

根据下面的图编写计算器,记得写明每个command代表什么。
符合要求的另有重谢,拜托了。

Dim min_len As Integer
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If

If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub

' Clear the current entry.
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
' Add a decimal point to the display.
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub

' Calculate the result of the previous operation.
Private Sub cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub

' Add a number to the display.
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = _
txtDisplay.Text & Format$(Index)
End If
End Sub

' Prepare to perform an operation.
Private Sub cmdOperator_Click(Index As Integer)
' Perform the previous operation.
cmdEquals_Click
' Remember this operation.
Operator = Index
' Start a new value.
NewEntry = True
End Sub

' Change the value's sign.
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub

' Check for normal characters.
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
' Check for unusual characters.
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub

' Keep the cursor on the right.
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub

' Keep the cursor on the right.
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub

' Check for normal characters.
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub

' Check for unusual characters.
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub

如果要实例的话,去网上找,顺便进我的Q群里,去学习吧:21941971
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-12
Dim TmpNum As Single, Flag As Integer, Point_1 As Boolean

Private Sub cmdMain_Click(Index As Integer)
On Error Resume Next

Select Case Index
Case 0 To 9
If cmdMain(Index) Then txtResult = txtResult & cmdMain(Index).Caption
Case 10
If Point_1 = True Then
Exit Sub
Else
txtResult = txtResult & "."
Point_1 = True
End If
Case 11
txtResult = -txtResult
Case 12 To 15, 19
TmpNum = 0
Flag = 0
TmpNum = Val(txtResult)
txtResult = ""
Flag = Index
Case 16
Point_1= False
Select Case Flag
Case 12
txtResult = Str(TmpNum + Val(txtResult))
Case 13
txtResult = Str(TmpNum - Val(txtResult))
Case 14
txtResult = Str(TmpNum * Val(txtResult))
Case 15
If Val(txtResult) = 0 Then txtResult = "除数不能为0"
txtResult = Str(TmpNum / Val(txtResult))
Case 19
If Val(txtResult) = 0 Then txtResult = "除数不能为0"
txtResult = Str(TmpNum Mod Val(txtResult))
End Select
Case 17
If Sgn(txtResult) = 1 Then
txtResult = Sqr(Val(txtResult))
Else
cmdMain(18).Value = True
txtResult = "输入函数错误"
End If
Case 18
txtResult = ""
Point_1= False
Flag = 0
End Select
End Sub

Private Sub Form_Load()
Point_1 = False
End Sub

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网