Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
#Include "Win32API.inc"
%IDC_Price = 800
%IDC_Rate = 801
%IDC_Months = 802
%IDC_Payments = 803
%IDC_Button = 804
Type Finance
price(4) As Single
rate(4) As Single
months(4) As Long
payments(4) As Single
End Type
#Resource Icon, logo, "dollar.ico"
Global hDlg As Dword, History() As Finance
Function PBMain() As Long
Dialog New Pixels, 0, "Payment Calculator",300,300,300,200, %WS_OverlappedWindow To hDlg
Dialog Set Icon hDlg, "logo"
Control Add Button, hDlg, %IDC_Button,"Calculate Loan Payment:", 50,10,150,20
Control Add Label, hDlg, 701,"Price:", 30,40,140,20
Control Add Label, hDlg, 702,"Yearly Interest Rate (%):", 30,70,140,20
Control Add Label, hDlg, 703,"Number Months:", 30,100,140,20
Control Add Line, hDlg, 704, "", 50,130,200,5
Control Add Label, hDlg, 705,"Payments:", 30,150,140,20
Control Add ComboBox, hDlg, %IDC_Price, , 170,40,100,100, %WS_TabStop Or %CBS_DropDown
Control Add ComboBox, hDlg, %IDC_Rate, , 170,70,100,100, %WS_TabStop Or %CBS_DropDown
Control Add ComboBox, hDlg, %IDC_Months, , 170,100,100,100, %WS_TabStop Or %CBS_DropDown
Control Add ComboBox, hDlg, %IDC_Payments, , 170,150,100,100, %WS_TabStop Or %CBS_DropDown
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
ReDim History(3)
CreateHistoryArray
LoadHistoryArray
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_Button, %IdOk
Select Case Cb.CtlMsg
Case %BN_Clicked
Control Set Text hDlg, %IDC_Payments, Format$(GetPayment,"###,###,##0.00")
UpdateHistoryArray
? Str$(History(3).payments(0))
? Str$(History(3).payments(1))
? Str$(History(3).payments(2))
' LoadHistoryArray
End Select
End Select
End Select
End Function
Function GetPayment() As Single
Local payment, price, rate, months As Single, temp$
Control Get Text hDlg, %IDC_Price To temp$ : price = Val(temp$)
Control Get Text hDlg, %IDC_Rate To temp$ : rate = Val(temp$)
Control Get Text hDlg, %IDC_Months To temp$ : months = Val(temp$)
'M = P(1+r)n r / [(1+r)n-1]
payment = price * (1 + rate/1200)^months * rate/1200 / ((1 + rate/1200)^months-1)
Function = payment
End Function
Sub UpdateHistoryArray
Local i As Long, temp$
For i = 4 To 1 Step -1 : History(i) = History(i-1) : Next i
Control Get Text hDlg, %IDC_Price To temp$ : History(0).price(0) = Val(temp$)
Control Get Text hDlg, %IDC_Rate To temp$ : History(0).rate(0) = Val(temp$)
Control Get Text hDlg, %IDC_Months To temp$ : History(0).months(0) = Val(temp$)
Control Get Text hDlg, %IDC_Payments To temp$ : History(0).payments(0) = Val(temp$)
End Sub
Sub LoadHistoryArray
Local i As Long
For i = %IDC_Price To %IDC_Payments : ComboBox Reset hDlg, i : Next i 'clear all comboboxes
For i = 0 To 4 : ComboBox Add hDlg,%IDC_Price,Trim$(Str$(History(0).price(i))) : Next i
For i = 0 To 4 : ComboBox Add hDlg,%IDC_Rate,Trim$(Str$(History(1).rate(i))) : Next i
For i = 0 To 4 : ComboBox Add hDlg,%IDC_Months,Trim$(Str$(History(2).months(i))) : Next i
For i = 0 To 4 : ComboBox Add hDlg,%IDC_Payments,Trim$(Str$(History(3).payments(i))) : Next i
For i = %IDC_Price To %IDC_Payments : ComboBox Select hDlg, i, 1 : Next i 'clear all comboboxes
End Sub
Sub CreateHistoryArray
Local i,iPos As Long
Data 1000,2000,5000,10000,50000
Data 4,8,10,12,18
Data 12,24,36,48,60
Data 85.15,0,0,0,0
For i = 0 To 4 : Incr iPos : History(0).price(i) = Val(Read$(iPos)) : Next i
For i = 0 To 4 : Incr iPos : History(1).rate(i) = Val(Read$(iPos)) : Next i
For i = 0 To 4 : Incr iPos : History(2).months(i) = Val(Read$(iPos)) : Next i
For i = 0 To 4 : Incr iPos : History(3).payments(i) = Val(Read$(iPos)) : Next i
End Sub
'gbs_01375
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm