Posted by: Zeeshan Amjad | July 31, 2009

WPF Programming with F# Revisited


In the last program we just saw the first step to do WPF programming in F# and we just create few sample application. Now let’s take a look at little bit deeper and try to make something useful in WPF with F#.

Here we are going to make one simple program to demonstrate the concept of “Time Value of Money”. Here we are going to input the present value of the money, interest rate and no of year and program will calculate the future value of the money. Just for the simplicity we assumed there is only one time interest per year. Here is the simplest F# program to demonstrate this.

#light

open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media

type MyApplication() = inherit Application()

type MyWindow() = 
    inherit Window(Title = "Time Value of Money", 
        Height = 300.0, Width = 400.0) 

// Calculate Future Value of Money            
let calculate(futureValue : Label, 
                presentValue : TextBox, 
                rate : TextBox, year : TextBox) = 
    let present = Convert.ToDouble(presentValue.Text)
    let interestRate = Convert.ToDouble(rate.Text) / 100.0
    let noYears = Convert.ToDouble(year.Text)
    let future = present * System.Math.Pow(1.0 + interestRate, noYears)
    futureValue.Content <- future.ToString("C")
        
[<STAThread>]
do
 let window = new MyWindow()
 window.WindowStyle <- Windows.WindowStyle.ToolWindow
 window.ResizeMode <- Windows.ResizeMode.NoResize
 window.WindowStartupLocation <- 
    Windows.WindowStartupLocation.CenterScreen
 
 // create gradient brush
 let gBrush = new LinearGradientBrush()
 gBrush.StartPoint <- new Point(0.0, 0.0)
 gBrush.EndPoint <- new Point(1.0, 1.0)
 gBrush.GradientStops.Add(new GradientStop(Colors.AliceBlue, 0.0))
 gBrush.GradientStops.Add(new GradientStop(Colors.Blue, 1.0))
 
 
 let grid = new Grid()
 grid.Background <- gBrush
 
 let row1 = new Controls.RowDefinition()
 let row2 = new Controls.RowDefinition()
 let row3 = new Controls.RowDefinition()
 let row4 = new Controls.RowDefinition()
 let row5 = new Controls.RowDefinition()
 
 let col1 = new Controls.ColumnDefinition()
 let col2 = new Controls.ColumnDefinition()
 
 grid.RowDefinitions.Add(row1)
 grid.RowDefinitions.Add(row2)
 grid.RowDefinitions.Add(row3)
 grid.RowDefinitions.Add(row4)
 grid.RowDefinitions.Add(row5)
 
 grid.ColumnDefinitions.Add(col1)
 grid.ColumnDefinitions.Add(col2)
 
 // present value amount label
 let lblPresent = new Label()
 lblPresent.Margin <- new Thickness(5.0)
 lblPresent.VerticalAlignment <- Windows.VerticalAlignment.Center
 lblPresent.Content <- "Present Value"
 Grid.SetRow(lblPresent, 0)
 Grid.SetColumn(lblPresent, 0)

 // present value amount Text
 let txtPresent = new TextBox()
 txtPresent.Margin <- new Thickness(5.0)
 txtPresent.VerticalAlignment <- Windows.VerticalAlignment.Center
 Grid.SetRow(txtPresent, 0)
 Grid.SetColumn(txtPresent, 1)
 
 // interest rate label
 let lblRate = new Label()
 lblRate.Margin <- new Thickness(5.0)
 lblRate.VerticalAlignment <- Windows.VerticalAlignment.Center
 lblRate.Content <- "Interest Rate"
 Grid.SetRow(lblRate, 1)
 Grid.SetColumn(lblRate, 0)
  
 // interest rate Text
 let txtRate = new TextBox()
 txtRate.Margin <- new Thickness(5.0)
 txtRate.VerticalAlignment <- Windows.VerticalAlignment.Center
 Grid.SetRow(txtRate, 1)
 Grid.SetColumn(txtRate, 1)
 
 // No of years label
 let lblYears = new Label()
 lblYears.Margin <- new Thickness(5.0)
 lblYears.VerticalAlignment <- Windows.VerticalAlignment.Center
 lblYears.Content <- "No of Years"
 Grid.SetRow(lblYears, 2)
 Grid.SetColumn(lblYears, 0)
 
 // No of years Text
 let txtYears = new TextBox()
 txtYears.Margin <- new Thickness(5.0)
 txtYears.VerticalAlignment <- Windows.VerticalAlignment.Center
 Grid.SetRow(txtYears, 2)
 Grid.SetColumn(txtYears, 1)

 // Future value label
 let lblFuture = new Label()
 lblFuture.Margin <- new Thickness(5.0)
 lblFuture.VerticalAlignment <- Windows.VerticalAlignment.Center
 lblFuture.Content <- "Future Value"
 Grid.SetRow(lblFuture, 3)
 Grid.SetColumn(lblFuture, 0)

 // Future value label
 let lblFutureValue = new Label()
 lblFutureValue.Margin <- new Thickness(5.0)
 lblFutureValue.VerticalAlignment <- Windows.VerticalAlignment.Center
 lblFutureValue.Content <- ""
 Grid.SetRow(lblFutureValue, 3)
 Grid.SetColumn(lblFutureValue, 1)
   
 // button to exit
 let btnExit = new Button()
 btnExit.Content <- "Exit"
 btnExit.Margin <- new Thickness(5.0)
 btnExit.VerticalAlignment <- Windows.VerticalAlignment.Center
 btnExit.Click.Add(fun _ -> window.Close()) |> ignore
 Grid.SetRow(btnExit, 4)
 Grid.SetColumn(btnExit, 0)

 // button to exit
 let btnCalculate = new Button()
 btnCalculate.Content <- "Calculate"
 btnCalculate.Margin <- new Thickness(5.0)
 btnCalculate.VerticalAlignment <- Windows.VerticalAlignment.Center
 btnCalculate.Click.Add(fun _ -> 
    calculate(lblFutureValue, txtPresent, txtRate, txtYears) )
 Grid.SetRow(btnCalculate, 4)
 Grid.SetColumn(btnCalculate, 1)
          
 grid.Children.Add(lblPresent) |> ignore
 grid.Children.Add(txtPresent) |> ignore
 grid.Children.Add(lblRate) |> ignore
 grid.Children.Add(txtRate) |> ignore
 grid.Children.Add(lblYears) |> ignore
 grid.Children.Add(txtYears) |> ignore
 grid.Children.Add(lblFuture) |> ignore
 grid.Children.Add(lblFutureValue) |> ignore
 grid.Children.Add(btnExit) |> ignore
 grid.Children.Add(btnCalculate) |> ignore
  
 window.Content <- grid
 
 let app = new MyApplication() in 
 app.Run(window) |> ignore

Here is the output of the program.

FSharpeWpf

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: