简单计算器源代码

1.完成程序界面如下:

 

 

2.源代码:

 

 

 

 


Option Explicit

 

Private Declare Function HideCaret Lib "user32" (ByVal hwnd As Long) As Long

 

 

 

Dim ONOFF As Boolean

 

 

 

Dim operator1 As String

 

Dim operator2 As String

 

Dim result

 

Dim opt As Integer

 

Dim op As Boolean

 

Dim caculated As Boolean

 

 

 

Private Sub aboutComputer_Click()

 

frmAbout.Show

 

End Sub

 

 

 

Private Sub cmdBackspace_Click()

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

Dim length As Integer

 

 

 

If op Then

 

    length = Len(operator2)

 

    If length <= 1 Then

 

            operator2 = ""

 

            Text2.Text = "0."

 

    Else

 

      operator2 = Mid$(operator2, 1, length - 1)

 

    Text2.Text = operator2

 

    End If

 

Else

 

    length = Len(operator1)

 

    

 

    If length <= 1 Then

 

        operator1 = ""

 

       Text2.Text = "0."

 

    Else

 

      operator1 = Mid$(operator1, 1, length - 1)

 

       Text2.Text = operator1

 

    End If

 

   

 

End If   

 

End Sub

 

 

 

Private Sub cmdClear_Click()

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

Text2.Text = "0."

 

operator1 = ""

 

operator2 = ""

 

opt = 99

 

op = False

 

End Sub

 

 

 

Private Sub cmdClear1_Click()

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

Text2.Text = "0."

 

operator1 = ""

 

operator2 = ""

 

opt = 99

 

op = False

 

 

 

End Sub

 

 

 

Private Sub cmdONOFF_Click()

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

Dim i As Integer

 

 

 

If ONOFF Then

 

    cmdONOFF.Caption = "ON"

 

    ONOFF = False

 

    For i = 0 To 10

 

     Command1(i).Enabled = False

 

     Next

 

     For i = 0 To 4

 

      Command2(i).Enabled = False

 

      Next

 

      cmdClear.Enabled = False

 

      cmdBackspace.Enabled = False

 

      cmdClear1.Enabled = False

 

      Text2.Alignment = 2

 

     operator1 = ""

 

    operator2 = ""

 

    opt = 99

 

    op = False

 

      Text2.Text = "you have locked the caculate!"

 

Else

 

    cmdONOFF.Caption = "OFF"

 

    ONOFF = True

 

     For i = 0 To 10

 

     Command1(i).Enabled = True

 

     Next

 

     For i = 0 To 4

 

      Command2(i).Enabled = True

 

      Next

 

      cmdClear.Enabled = True

 

      cmdBackspace.Enabled = True

 

      cmdClear1.Enabled = True

 

      Text2.Alignment = 1

 

      Text2.Text = "0."

 

End If

 

End Sub

 

 

 

Private Sub Command1_Click(Index As Integer)

 

 

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

Dim dot, value

 

 

 

If op Then

 

   

 

   

 

   If caculated Then

 

   operator2 = ""

 

   caculated = False

 

   End If  

 

  

 

   If Index = 0 Then

 

       dot = InStr(1, operator2, ".")

 

       value = Val(operator2)

 

        If value <> 0 Or dot <> 0 Then

 

            operator2 = operator2 & Index

 

            Text2.Text = operator2

 

        End If

 

    Else

 

    If Index <> 10 Then

 

    operator2 = operator2 & Index

 

   

 

    Text2.Text = operator2

 

    Else

 

   dot = InStr(1, operator2, ".")

 

        If dot = 0 Then

 

            value = Val(operator2)

 

            If value = 0 Then

 

                operator2 = "0."

 

            Else

 

            operator2 = operator2 & "."

 

            End If

 

            Text2.Text = operator2

 

           

 

        Else

 

            Text2.Text = operator2

 

        End If

 

    End If   

 

End If

 

Else

 

 

 

    If Index = 0 Then

 

       dot = InStr(1, operator1, ".")

 

       value = Val(operator1)

 

        If value <> 0 Or dot <> 0 Then

 

            operator1 = operator1 & Index

 

            Text2.Text = operator1

 

        End If

 

    Else

 

    If Index <> 10 Then

 

    operator1 = operator1 & Index

 

   

 

    Text2.Text = operator1

 

    Else

 

   dot = InStr(1, operator1, ".")

 

        If dot = 0 Then

 

            value = Val(operator1)

 

            If value = 0 Then

 

                operator1 = "0."

 

            Else

 

            operator1 = operator1 & "."

 

            End If

 

            Text2.Text = operator1

 

           

 

        Else

 

            Text2.Text = operator1

 

        End If

 

    End If

 

   

 

End If

 

   

 

End If

 

End Sub

 

 

 

Private Sub Command2_Click(Index As Integer)

 

Text2.SetFocus

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

If Index = 4 Then

 

    If op Then

 

       

 

        operator2 = Val(operator2)

 

       

 

        Select Case opt

 

        Case 0

 

            result = Val(operator1) + Val(operator2)

 

        Case 1

 

            result = Val(operator1) - Val(operator2)

 

        Case 2

 

            result = Val(operator1) * Val(operator2)

 

        Case 3

 

           

 

             If operator2 = 0 Then

 

                result = "除数不能为零!"

 

             Else

 

                 result = Val(operator1) / Val(operator2)

 

            End If

 

        End Select

 

         If result < 0 Then

 

           

 

            If Abs(result) < 1 Then

 

                Text2.Text = "-0" & Abs(result)

 

            Else

 

                Text2.Text = result

 

            End If

 

        Else           

 

        If result < 1 And result > 0 Then

 

            Text2.Text = "0" & result

 

           

 

        Else

 

        Text2.Text = result           

 

        End If

 

        operator1 = result

 

        op = True

 

       caculated = True

 

        End If

 

    End If

 

Else

 

    op = True

 

    opt = Index

 

End If

 

End Sub

 

 

 

Private Sub Form_Load()

 

ONOFF = True

 

 

 

HideCaret (Text1.hwnd)

 

HideCaret (Text2.hwnd)

 

 

 

operator1 = ""

 

operator2 = ""

 

opt = 90

 

op = False

 

caculated = False

 

End Sub

 

 

 

Private Sub helpTopic_Click()

 

frmSplash.Show

 

End Sub

 

 

 

Private Sub Text1_GotFocus()

 

HideCaret (Text1.hwnd)

 

End Sub

 

 

 

Private Sub Text1_KeyPress(KeyAscii As Integer)

 

HideCaret (Text1.hwnd)

 

KeyAscii = 0

 

End Sub

 

 

 

Private Sub Text2_GotFocus()

 

HideCaret (Text2.hwnd)

 

End Sub

 

 

 

Private Sub Text2_KeyPress(KeyAscii As Integer)

 

HideCaret (Text2.hwnd)

 

KeyAscii = 0

 

End Sub

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2005-06-08 10:53  h2内存数据库  阅读(727)  评论(0编辑  收藏  举报

导航