Visual F# 100 Examples: Example Number 16(Search a Record Value)

Problem: Make a Windows Forms Application that will allow the user to search a record value from a database file.

// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System.Collections.Generic
open System
open System.Windows.Forms
open System.ComponentModel
open System.Data
open System.Drawing
open System.Data.OleDb
//creates a font
let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)  
//creates a connection object
let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
  Data Source=C:\Documents and Settings\Station03\My Documents\dbEmployee.mdb")
 //creates an OleDbDataAdapter
let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)
//generates a dataset
let dataset11 = new DataSet()
//fills the dataset with recod values
dataadpter.Fill(dataset11,"tblEmployee")|>ignore
//creates a form
let dataform = new Form(Text="Search a Record Value",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(400, 360),StartPosition=FormStartPosition.CenterScreen)
//creates our controls
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(300, 320)) 
let searchbutton=new Button(Text="Search", Location=new System.Drawing.Point(220, 320))  
let label1=new Label(Text="Enter the employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)
let label2=new Label(Text="Empno:",Location=new System.Drawing.Point(0, 50),AutoSize=true)
let label3=new Label(Text="Firstname:",Location=new System.Drawing.Point(0,100),AutoSize=true)
let label4=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,150),AutoSize=true)
let empnotext=new TextBox(Location=new System.Drawing.Point(200,10))
let emplabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)
let fnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)
let lnamelabel=new Label(Location=new System.Drawing.Point(100,150),BorderStyle=BorderStyle.FixedSingle)
//creates a datagrid
let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(300, 120),Location=new System.Drawing.Point(10, 180))
//creates a grid control colums
let chrempnocol=new DataGridViewTextBoxColumn()
let chrfnamecol=new DataGridViewTextBoxColumn()
let chrlnamecol=new DataGridViewTextBoxColumn()
//adds the columns into our datagrid
datagrid.Columns.Add(chrempnocol)|>ignore
datagrid.Columns.Add(chrfnamecol)|>ignore
datagrid.Columns.Add(chrlnamecol)|>ignore
datagrid.DataSource <- dataset11.Tables.["tblEmployee"]
//assingns the font to our form
dataform.Font<-ffont
//links our fieldname to each grid
//and change its header text
chrempnocol.DataPropertyName<-"chrempno"
chrempnocol.HeaderText<-"Employee No."
chrfnamecol.DataPropertyName<-"chrfname"
chrfnamecol.HeaderText<-"First Name"
chrlnamecol.DataPropertyName<-"chrlname"
chrlnamecol.HeaderText<-"Last Name"
//add the datagrid to our form
dataform.Controls.Add(datagrid)
//adds the controls to our form
dataform.Controls.Add(exitbutton)
dataform.Controls.Add(searchbutton)
dataform.Controls.Add(label1)
dataform.Controls.Add(label2)
dataform.Controls.Add(label3)
dataform.Controls.Add(label4)
dataform.Controls.Add(empnotext)
dataform.Controls.Add(emplabel)
dataform.Controls.Add(fnamelabel)
dataform.Controls.Add(lnamelabel)
//binds the fieldnames to our label
emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))
fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))
lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))
searchbutton.Click.Add(fun search->  
                    //handles the row index number               
                    let mutable introws=0
                    //determines if the record has been found or not
                    let mutable blnfound=false   
                    //handles the total number of records                
                    let mutable inttotrec=Convert.ToInt32(dataset11.Tables.["tblEmployee"].Rows.Count)
                    //handles the data inputted by the user
                    let strtext=Convert.ToString(empnotext.Text)
                    //while no match is found and the end of the file has not been reached
                    while((blnfound=false) && (introws<=inttotrec-1)) do
                         let strempnum=Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(introws).Item(0))
                         //compare the data inputted in the textbox to the employee number in our table
                         //if they are equal, display the match record
                         if strtext.ToUpper()=strempnum.ToUpper() then
                            blnfound<-true
                            emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(introws).Item(0))
                            fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(introws).Item(1))
                            lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(introws).Item(2)) 
                         //compare to the next record while no match is found
                         introws<-introws + 1    
                    //if no match is found, display this      
                    if blnfound=false then
                        MessageBox.Show("Record not found.","Search a Record Value",MessageBoxButtons.OK,MessageBoxIcon.Information)|>ignore)                       
//when the exit button is clicked
exitbutton.Click.Add(fun exit->
//close the form and dataconnection
                    dataform.Close()
                    oleconn.Close()) 
//executes our application
Application.Run(dataform)

Since Visual F# is very particular with the code indention, I posted below the screenshot of our code in the while loop section.



Here's what it should look like if you click the run icon:

Visual F# 100 Examples: Example 14 and 15(Pattern Matching)

Pattern matching/match with statement is similar to the switch selection statement in other programming languages. The following examples demonstrate how to use it:


Problem: Make an application that will asks a letter and displays its equivalent U. S. military phonetic alphabet.
//use F# library
open System
//change the CLI title
System.Console.Title<-"Display Military Phoenitic Alphabet"
//adds color to our console application
System.Console.ForegroundColor<-ConsoleColor.Blue
//asks the user to enter a letter
printfn "Enter a letter:"
//convert the input to character and convert it to uppercase letter
//this is just for comparison purpose
let chrletter=Char.ToUpper(Convert.ToChar(System.Console.ReadLine()))
//clear the screen   
System.Console.Clear()
//match the value of chrletter to the ff. values
match chrletter with
//if the value of chrletter is a or A display Alpha
| 'A' ->printfn "Alpha"
//if the value of chrletter is b or B display Bravo
| 'B' ->printfn "Bravo"
//if the value of chrletter is c or C display Charlie
| 'C' ->printfn "Charlie"
//if the value of chrletter is d or D display Delta
| 'D' ->printfn "Delta"
//if the value of chrletter is e or E display Echo
| 'E'->printfn "Echo"
//if the value of chrletter is f or F display Foxtrot
| 'F'->printfn "FoxTrot"
//if the value of chrletter is g or G display Golf
| 'G'->printfn "Golf"
//if the value of chrletter is h or H display Hotel
| 'H'->printfn "Hotel"
//if the value of chrletter is i or I display India
| 'I'->printfn "India"
//if the value of chrletter is j or J display Juliet
| 'J'->printfn "Juliet"
//if the value of chrletter is k or K display Kilo
| 'K'->printfn "Kilo"
//if the value of chrletter is l or L display Lima
| 'L'->printfn "Lima"
//if the value of chrletter m or M display Mike
| 'M'->printfn "Mike"
//if the value of chrletter is n or N display November
| 'N'->printfn "November"
//if the value of chrletter is o or O display Oscar
| 'O'->printfn "Oscar"
//if the value of chrletter is p or P display Papa
| 'P'->printfn "Papa"
//if the value of chrletter is q or Q display Quebec
| 'Q'->printfn "Quebec"
//if the value of chrletter is r or R display Romeo
| 'R'->printfn "Romeo"
//if the value of chrletter is s or S display Sierra
| 'S'->printfn "Sierra"
//if the value of chrletter is t or T display Tango
| 'T'->printfn "Tango"
//if the value of chrletter is u or U display Uniform
| 'U'->printfn "Uniform"
//if the value of chrletter is v or V display Victor
| 'V'->printfn "Victor"
//if the value of chrletter is w or W display Whiskey
| 'W'->printfn "Whiskey"
//if the value of chrletter is x or X display X-Ray
| 'X'->printfn "X-Ray"
//if the value of chrletter is y or Y display Yankee
| 'Y'->printfn "Yankee"
//if the value of chrletter is z or Z display Zulu
| 'Z'->printfn "Zulu"
//otherwise
| _ ->printfn "Invalid input"
The last statement |_ is similar to the
default statement in other programming languages Switch conditional structure. It is automatically executed when no pattern match is found. Don't forget to add it at the end of every match with statement otherwise you will get an “Incomplete pattern matches on this expression” error.


Problem: Make an application that will ask the month number and display the corresponding month name and the number of days in it. Use pattern matching.

//use F# library
open System
//change the CLI title
System.Console.Title<-"Display Month Name"
//adds color to our console application
System.Console.ForegroundColor<-ConsoleColor.Blue
System.Console.BackgroundColor<-ConsoleColor.White
//asks the user to enter a month number
printfn "Enter a month number(1-12):"
let intmonth=Convert.ToInt32(System.Console.ReadLine())
//clear the screen   
System.Console.Clear()
//match the value of intmonth to the ff. values
match intmonth with
//if the value of intmonth is 1 display January
| 1 ->printfn "January(31 days)"
//if the value of intmonth is 2 display February
| 2 ->printfn "Febrary(28/29 days)"
//if the value of intmonth is 3 display March 
| 3 ->printfn "March(31 days)"
//if the value of intmonth is 4 display April
| 4 ->printfn "April(30 days)"
//if the value of intmonth is 5 display May
| 5 ->printfn "May(31 days)"
//if the value of intmonth is 6 display June
| 6 ->printfn "June(30 days)"
//if the value of intmonth is 7 display July
| 7 ->printfn "July(31 days)"
//if the value of intmonth is 8 display August
| 8 ->printfn "August(31 days)"
//if the value of intmonth is 9 display September
| 9 ->printfn "September(30 days)"
//if the value of intmonth is 10 display October
| 10 ->printfn "October(31 days)"
//if the value of intmonth is 11 display November
| 11 ->printfn "November(30 days)"
//if the value of intmonth is 12 display December
| 12->printfn "December(31 days)"
//otherwise
| _ ->printfn "Invalid input"

That's all for now my Visual F# friends. Ciao!