USES "UI"
'---Define a button ID
begin ControlID
%lblNum1
%txtNum1
%lblNum2
%txtNum2
%lblResult
%txtResult
%lblOp
%cbOp
%btnResult
%btnClose
end ControlID
string sOperations(4) = "+", "-", "*", "/"
dialog Font "Courier New", 12
DIALOG New Pixels , Name SimpleCalc, 0, "Basic calculator with events",-1,-1, 640, 380,
%WS_DLGFRAME | %DS_CENTER | %WS_CAPTION | %WS_SYSMENU | %WS_OVERLAPPEDWINDOW,
CONTROL ADD Label , Name lblNum1 , SimpleCalc.Handle, %lblNum1 , "Number 1" , 10, 15, 350, 20, %SS_NOTIFY
CONTROL ADD Textbox , Name txtNum1 , SimpleCalc.Handle, %txtNum1 , "" , 10, 30, 350, 30, %ES_AUTOHSCROLL | %ES_RIGHT | %WS_TABSTOP | %ES_NUMBER
CONTROL ADD Label , Name lblOp , SimpleCalc.Handle, %lblOp , "Operation" , 10, 60, 350, 20, %SS_NOTIFY
CONTROL ADD COMBOBOX, Name cbOp , SimpleCalc.Handle, %cbOp , sOperations , 10, 75, 350, 100, %CBS_DROPDOWNLIST | %WS_TABSTOP
CONTROL ADD Label , Name lblNum2 , SimpleCalc.Handle, %lblNum2 , "Number 2" , 10, 110, 350, 20, %SS_NOTIFY
CONTROL ADD Textbox , Name txtNum2 , SimpleCalc.Handle, %txtNum2 , "" , 10, 125, 350, 30, %ES_AUTOHSCROLL | %ES_RIGHT | %WS_TABSTOP '| %ES_NUMBER
CONTROL ADD Label , Name lblResult, SimpleCalc.Handle, %lblResult , "Result" , 10, 255, 350, 20, %SS_NOTIFY
CONTROL ADD Textbox , Name txtResult, SimpleCalc.Handle, %txtResult , "0" , 10, 270, 350, 30, %ES_AUTOHSCROLL | %ES_RIGHT | %WS_TABSTOP | %ES_READONLY
CONTROL ADD BUTTON , Name btnResult, SimpleCalc.Handle, %btnResult , "Result" , 480, 10, 150, 25, %BS_NOTIFY | %BS_CENTER | %BS_VCENTER | %WS_TABSTOP
CONTROL ADD BUTTON , Name btnClose , SimpleCalc.Handle, %btnClose , "Close" , 480, 340, 150, 25, %BS_NOTIFY | %BS_CENTER | %BS_VCENTER | %WS_TABSTOP
DIALOG SHOW MODAL SimpleCalc.Handle
'------------------------------------------------
' Callback function used to handle dialog events
' not handled by specific event functions
'------------------------------------------------
CallBack Function SimpleCalc_OnCallBack() As Long
'------------------------------------------------
End Function
'------------------------------------------------
CallBack Function SimpleCalc_OnLoad() As Long
'------------------------------------------------
'---Limits window minimum size
DIALOG SET MINSIZE SimpleCalc.Handle, 550, 320
'---Force resize logic
control set Resize CBHNDL, %btnResult , 0, 1, 0, 0
control set Resize CBHNDL, %btnClose , 0, 1, 0, 1
'---Select first element in combo operations
COMBOBOX SELECT SimpleCalc.Handle, %cbOp, 1
'---Some initial values
txtNum1.Text = "10"
txtNum2.Text = "20"
'---Change name to Close button
btnClose.Text = "Close Me"
End Function
'------------------------------------------------
' Function fired when SimpleCalc window will be closed
'------------------------------------------------
CallBack Function SimpleCalc_OnDestroy() As Long
'------------------------------------------------
MsgBox "Thanks for testing thinBASIC"
End Function
'------------------------------------------------
' Function fired when button Close is clicked
'------------------------------------------------
CallBack Function btnClose_OnClick() As Long
'------------------------------------------------
DIALOG End CBHNDL
End Function
'------------------------------------------------
' Function fired when drop down combo box is closed after a selection
'------------------------------------------------
callback function cbOp_ONCLOSEUP() as Long
'------------------------------------------------
CalculateResult
End Function
'------------------------------------------------
' Function fired when change selected item in a drop down combo box
'------------------------------------------------
callback function cbOp_ONSELCHANGE() as Long
'------------------------------------------------
CalculateResult
End Function
'------------------------------------------------
' Function fired when button Result is clicked
'------------------------------------------------
CallBack Function btnResult_OnClick() As Long
'------------------------------------------------
CalculateResult
End Function
'------------------------------------------------
' Function fired when text inside txtNum1 textbox is changed
'------------------------------------------------
CallBack Function txtNum1_OnChange() As Long
'------------------------------------------------
CalculateResult
End Function
'------------------------------------------------
' Function fired when text inside txtNum2 textbox is changed
'------------------------------------------------
CallBack Function txtNum2_OnChange() As Long
'------------------------------------------------
CalculateResult
End Function
'------------------------------------------------
' Calculate and fill txtResult textbox
'------------------------------------------------
Function CalculateResult() as ext
'------------------------------------------------
txtResult.BackColor = rgb(0, 255, 255)
select case cbOp.Text
case "+"
txtResult.Text = tstr$(val(txtNum1.text) + val(txtNum2.text))
case "-"
txtResult.Text = tstr$(val(txtNum1.text) - val(txtNum2.text))
case "*"
txtResult.Text = tstr$(val(txtNum1.text) * val(txtNum2.text))
case "/"
if val(txtNum2.text) Then
txtResult.Text = tstr$(val(txtNum1.text) / val(txtNum2.text))
Else
txtResult.BackColor = rgb(255, 0, 0)
txtResult.Text = "Div/0"
end If
end Select
function = txtResult.Text
End Function
Bookmarks