電卓(F-BASICのSAMPLEより)          <TOP>


F-BASIC63のSAMPLEです。短いプログラムに納得の手法が見てとれます。

declare sub Button1_on edecl ()

sub Button1_on()

    処理

end sub

これをボタンの数だけ並べると、大変ですヨネ・・

プロパティで

数字Buttonのイベントを全てButtonNum_onとし、クリックされたButtonのTextを取得しval関数で数値に変換しています。

加減乗除およびクリアButtonのイベントは全てButtonOpl_onとし、クリックされたButtonのTextを取得し加減乗除・クリアを実行しています。

フォームの体裁のみ変更しています。

 

'================================================
'= 電卓のサンプルプログラム(富士通ミドルウェアSampleより)
'================================================
#include "Windows.bi"

Var Shared Text1 As Object
Var Shared FF
Var Shared Num1
Var Shared Num2
FF = 0

Text1.Attach GetDlgItem("Text1")
Text1.SetWindowText "0"

'================================================
'=
'================================================
Declare Sub ButtonOpl_on edecl ()
Sub ButtonOpl_on ()
    Var Button As Object
    Static Opl$
    Button.Attach GETFOCUS
    FF = 1
    Select Case Button.GetWindowText
        Case "C"
            Text1.SetWindowText "0"
        Case "AC"
            Text1.SetWindowText "0"
            FF = 0
        Case "+", "-", "*",  "/"
            Select Case Opl$
                Case "+"
                    Num2 = Val(Text1.GetWindowText)
                    Num1 = Num1 + Num2
                    Text1.SetWindowText Mid$(Str$(Num1), 2)
                Case "-"
                    Num2 = Val(Text1.GetWindowText)
                    Num1 = Num1 - Num2
                    Text1.SetWindowText Mid$(Str$(Num1), 2)
                Case "*"
                    Num2 = Val(Text1.GetWindowText)
                    Num1 = Num1 * Num2
                    Text1.SetWindowText Mid$(Str$(Num1), 2)
                Case "/"
                    Num2 = Val(Text1.GetWindowText)
                    Num1 = Num1 / Num2
                    Text1.SetWindowText Mid$(Str$(Num1), 2)
            End Select
            Opl$ = Button.GetWindowText
        Case "="
            Num2 = Val(Text1.GetWindowText)
            Select Case Opl$
                Case "+"
                    Num1 = Num1 + Num2
                Case "-"
                    Num1 = Num1 - Num2
                Case "*"
                    Num1 = Num1 * Num2
                Case "/"
                    Num1 = Num1 / Num2
            End Select
            Text1.SetWindowText Mid$(Str$(Num1), 2)
            Opl$ = ""
    End Select
End Sub

'================================================
'=
'================================================
Declare Sub ButtonNum_on edecl ()
Sub ButtonNum_on ()
    Var Button As Object
    Button.Attach GETFOCUS
    BNum$ = Button.GetWindowText
    Num$ = Text1.GetWindowText
    If Num$ = "0" Then
        Text1.SetWindowText BNum$
        FF = 0
    Else If FF = 1 Then
        Num1 = Val(Num$)
        FF = 0
        Text1.SetWindowText BNum$
    Else
        Text1.SetWindowText Num$ + BNum$
    End If
End Sub

'================================================
'=
'================================================
Declare Sub Copy_on edecl ()
Sub Copy_on()
    SetCbText Text1.GetWindowText
End Sub

'================================================
'=
'================================================
Declare Sub Exit_on edecl ()
Sub Exit_on()
    End
End Sub

'================================================
'=
'================================================
While 1
    WaitEvent
Wend
Stop
End