Visual F# 100 Examples: Example Number 6

Problem. Create an application that will display the surface area of a cube. The surface area of a cube can be computed by multiplying the inputted side by itself and multiplying the product by 6.
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates a new form  
  10. let cubeform=new Form(Text="Compute the Surface Area of a Cube", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates our controls  
  12. let n1label=new Label(Text="Enter a side value:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  13. let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))  
  14. let n2label=new Label(Text="Surface area of a cube:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  15. //creates a label that will display the result of the computation  
  16. let anslabel=new Label(Location=new System.Drawing.Point(140, 90), BorderStyle=BorderStyle.FixedSingle)  
  17. //make our buttons  
  18. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))  
  19. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  20. //add the controls into the form  
  21. cubeform.Controls.Add(n1label)  
  22. cubeform.Controls.Add(firsttextbox)  
  23. cubeform.Controls.Add(n2label)  
  24. cubeform.Controls.Add(anslabel)  
  25. cubeform.Controls.Add(computebutton)  
  26. cubeform.Controls.Add(exitbutton)  
  27.   
  28. //when the compute button is clicked  
  29. computebutton.Click.Add(fun ans->  
  30. let sidevalue=Convert.ToDouble(firsttextbox.Text)  
  31. let areaofacube=6.00*(sidevalue*sidevalue)  
  32.   
  33. //display the areaofacube value in the anslabel  
  34. anslabel.Text<-Convert.ToString(areaofacube))  
  35.  //when the exit button is clicked, close the form              
  36. exitbutton.Click.Add(fun exit -> cubeform.Close())    
  37. Application.Run(cubeform)  

We'll be adding some new controls after our tenth example:)

Visual F# 100 Examples:Example Number 5

Problem: Design an application that will ask a length value, height value, and width value and display the volume of the prism.
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates our controls  
  10. let prismform=new Form(Text="Compute the Volume of a Prism", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Length value:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let lenghttxtbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Width value:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let widthtxtbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Height value:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let heighttxtbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17.   
  18. let n4label=new Label(Text="Prisms volume:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  19. let prismlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  20. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  21. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  22.   
  23. //add the controls into the form  
  24. prismform.Controls.Add(n1label)  
  25. prismform.Controls.Add(lenghttxtbox)  
  26. prismform.Controls.Add(n2label)  
  27. prismform.Controls.Add(widthtxtbox)  
  28. prismform.Controls.Add(n3label)  
  29. prismform.Controls.Add(heighttxtbox)  
  30. prismform.Controls.Add(n4label)  
  31. prismform.Controls.Add(prismlabel)  
  32. prismform.Controls.Add(computebutton)  
  33. prismform.Controls.Add(exitbutton)  
  34.   
  35. //when the compute button is clicked  
  36. computebutton.Click.Add(fun compute->  
  37. //assign the data inputted in each control to each corresponding variables  
  38. //compute the volume using the formula volume=lengthvalue*widthvalue*heightvalue  
  39. //display the result on outcome on their assigned controls  
  40. let lenghtvalue=Convert.ToDouble(lenghttxtbox.Text)  
  41. let widthvalue=Convert.ToDouble(widthtxtbox.Text)  
  42. let heightvalue=Convert.ToDouble(heighttxtbox.Text)  
  43. let prismvol=lenghtvalue*widthvalue*heightvalue  
  44. prismlabel.Text<-Convert.ToString(prismvol))  
  45.  //when the exit button is clicked, close the form              
  46. exitbutton.Click.Add(fun exit -> prismform.Close())    
  47. Application.Run(prismform)  

Visual F# 100 Examples: Example Number 4

Problem: Make an application that will display the area of an annulus (overlapping circles). The area of an annulus can be calculated by subtracting the area of a small circle from the area of a larger circle.

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates a new form  
  10. let annulform=new Form(Text="Dislay the Area of an Annulus", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates our controls  
  12. let n1label=new Label(Text="Area of a large circle:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  13. let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))  
  14. let n2label=new Label(Text="Area of a small circle:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  15. let secondtextbox=new TextBox(Location=new System.Drawing.Point(120,50))  
  16. let n3label=new Label(Text="Area of an annulus:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  17. //creates a label that will display the result of the computation  
  18. let anslabel=new Label(Location=new System.Drawing.Point(120, 90), BorderStyle=BorderStyle.FixedSingle)  
  19. //make our buttons  
  20. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))  
  21. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  22. //add the controls into the form  
  23. annulform.Controls.Add(n1label)  
  24. annulform.Controls.Add(firsttextbox)  
  25. annulform.Controls.Add(n2label)  
  26. annulform.Controls.Add(secondtextbox)  
  27. annulform.Controls.Add(n3label)  
  28. annulform.Controls.Add(anslabel)  
  29. annulform.Controls.Add(computebutton)  
  30. annulform.Controls.Add(exitbutton)  
  31.   
  32. //when the compute button is clicked  
  33. computebutton.Click.Add(fun ans ->  
  34. let areaoflargecircle=Convert.ToDouble(firsttextbox.Text)  
  35. let areaofsmallcircle=Convert.ToDouble(secondtextbox.Text)  
  36. let areaofannulus=areaoflargecircle-areaofsmallcircle  
  37.   
  38. //display the area of annulus value in the anslabel  
  39. anslabel.Text<-Convert.ToString(areaofannulus))  
  40.  //when the exit button is clicked, close the form              
  41. exitbutton.Click.Add(fun exit -> annulform.Close())    
  42. Application.Run(annulform)  

Visual F# 100 Examples:Example Number 3

We are now on our third example of our planned 100 Visual F# windows form applications learning examples. If you noticed we are just using basic controls such as textboxes, labels, and buttons. We will add some new controls in our future examples. Meanwhile, try the following:

Problem: Create an application that will ask the name of an object or material, its voltage and current values then display its electrical resistance.

Solution:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates our controls  
  10. let resistanceform=new Form(Text="Compute Resistance", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Material:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let materialtextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Current(volts):", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let currenttextbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Voltage(amperes):", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let voltstextbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17.   
  18. let n4label=new Label(Text="Material:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  19. let materiallabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  20. let n5label=new Label(Text="Resistance(ohms):", Location=new System.Drawing.Point(0, 210),AutoSize=true)  
  21. let resistancelabel=new Label(Location=new System.Drawing.Point(130, 210),BorderStyle=BorderStyle.FixedSingle)  
  22. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  23. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  24.   
  25. //add the controls into the form  
  26. resistanceform.Controls.Add(n1label)  
  27. resistanceform.Controls.Add(materialtextbox)  
  28. resistanceform.Controls.Add(n2label)  
  29. resistanceform.Controls.Add(currenttextbox)  
  30. resistanceform.Controls.Add(n3label)  
  31. resistanceform.Controls.Add(voltstextbox)  
  32. resistanceform.Controls.Add(n4label)  
  33. resistanceform.Controls.Add(materiallabel)  
  34. resistanceform.Controls.Add(n5label)  
  35. resistanceform.Controls.Add(resistancelabel)  
  36. resistanceform.Controls.Add(computebutton)  
  37. resistanceform.Controls.Add(exitbutton)  
  38.   
  39. //when the compute button is clicked  
  40. computebutton.Click.Add(fun compute->  
  41. //assign the data inputted in each control to each corresponding variables  
  42. //compute the resistance using the formula resistance=voltage divided by current  
  43. //display the result on outcome on their assigned controls  
  44. let material=materialtextbox.Text  
  45. let current=Convert.ToDouble(currenttextbox.Text)  
  46. let voltage=Convert.ToDouble(voltstextbox.Text)  
  47. let resistance=voltage/current  
  48. materiallabel.Text<-material  
  49. resistancelabel.Text<-Convert.ToString(resistance))  
  50.  //when the exit button is clicked, close the form              
  51. exitbutton.Click.Add(fun exit -> resistanceform.Close())    
  52. Application.Run(resistanceform)  

Visual F# 100 Examples: Example Number 2

Last time I have shared to you our first simple example of window forms application using Visual F#, let’s now proceed to our second example:

Problem: Make a windows forms application that will ask the amount of consumption, amount of investment, government spending, amount of exports and imports and display the net emports and Gross Domestic Product (GDP).

Solution:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates our controls  
  10. let gdpform=new Form(Text="Compute GDP", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Consumption:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Investment:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let secondtextbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Government spending:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let thirdtextbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17. let n4label=new Label(Text="Exports:", Location=new System.Drawing.Point(0, 120),AutoSize=true)  
  18. let fourthtextbox=new TextBox(Location=new System.Drawing.Point(130, 120))  
  19. let n5label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 150),AutoSize=true)  
  20. let fifthtextbox=new TextBox(Location=new System.Drawing.Point(130, 150))  
  21. let n6label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  22. let netemlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  23. let n7label=new Label(Text="GDP:", Location=new System.Drawing.Point(0, 210),AutoSize=true)  
  24. let gdplabel=new Label(Location=new System.Drawing.Point(130, 210),BorderStyle=BorderStyle.FixedSingle)  
  25. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  26. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  27. //add the controls into the form  
  28. gdpform.Controls.Add(n1label)  
  29. gdpform.Controls.Add(firsttextbox)  
  30. gdpform.Controls.Add(n2label)  
  31. gdpform.Controls.Add(secondtextbox)  
  32. gdpform.Controls.Add(n3label)  
  33. gdpform.Controls.Add(thirdtextbox)  
  34. gdpform.Controls.Add(n4label)  
  35. gdpform.Controls.Add(fourthtextbox)  
  36. gdpform.Controls.Add(n5label)  
  37. gdpform.Controls.Add(fifthtextbox)  
  38. gdpform.Controls.Add(n6label)  
  39. gdpform.Controls.Add(netemlabel)  
  40. gdpform.Controls.Add(n7label)  
  41. gdpform.Controls.Add(gdplabel)  
  42. gdpform.Controls.Add(computebutton)  
  43. gdpform.Controls.Add(exitbutton)  
  44. //when the compute button is clicked  
  45. computebutton.Click.Add(fun compute->  
  46. //assigned the numbers inputted to each textbox to their corresponding variable  
  47. //compute the net emport using the formula net emport=export-import  
  48. //compute the gdp using the formula gdp=consumption+investment+govspending+netemport  
  49. let consumption=Convert.ToDouble(firsttextbox.Text)  
  50. let investment=Convert.ToDouble(secondtextbox.Text)  
  51. let govspending=Convert.ToDouble(thirdtextbox.Text)  
  52. let export=Convert.ToDouble(fourthtextbox.Text)  
  53. let import=Convert.ToDouble(fifthtextbox.Text)  
  54. let netemport=export-import  
  55. let gdp=(consumption+investment+govspending+netemport)  
  56. netemlabel.Text<-Convert.ToString(netemport)  
  57.                  gdplabel.Text<-Convert.ToString(gdp))  
  58.  //when the exit button is clicked, close the form              
  59. exitbutton.Click.Add(fun exit -> gdpform.Close())    
  60. Application.Run(gdpform)  

Visual F# 100 Examples: Example Number 1

Starting today, I will be sharing to you guys some learning examples that will aid you in learning windows forms application programming in Visual F#. Our target is to be able to share at least 100 examples. Let’s start with example number 1:

Problem: Make a windows form application that will ask the user’s mental age and chronological age and display his intelligence quotient (IQ).

Solution:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates a new form  
  10. let iqform=new Form(Text="Compute IQ", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates a label  
  12. let n1label=new Label(Text="Mental age:",Top=20,Left=5,AutoSize=true)  
  13. let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  14. //creates another label and change its text to “Second number:”  
  15. let n2label=new Label(Text="Chronological age:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  16. let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50))  
  17. //creates another label and change its text to sum  
  18. let n3label=new Label(Text="IQ:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  19. //creates a label that will display the result of the computation  
  20. let anslabel=new Label(Location=new System.Drawing.Point(80, 90), BorderStyle=BorderStyle.FixedSingle)  
  21. //make our buttons  
  22. let addbutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))  
  23. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  24. //add the controls into the form  
  25. iqform.Controls.Add(n1label)  
  26. iqform.Controls.Add(firsttextbox)  
  27. iqform.Controls.Add(n2label)  
  28. iqform.Controls.Add(secondtextbox)  
  29. iqform.Controls.Add(n3label)  
  30. iqform.Controls.Add(anslabel)  
  31. iqform.Controls.Add(addbutton)  
  32. iqform.Controls.Add(exitbutton)  
  33.   
  34. //when the compute button is clicked  
  35. addbutton.Click.Add(fun addfunction ->  
  36. let manum=Convert.ToDouble(firsttextbox.Text)  
  37. let canum=Convert.ToDouble(secondtextbox.Text)  
  38. let iq=Convert.ToDouble((manum/canum)*100.00)  
  39.   
  40. //display the iq value in the anslabel  
  41. anslabel.Text<-Convert.ToString(iq))  
  42.  //when the exit button is clicked, close the form              
  43. exitbutton.Click.Add(fun exit -> iqform.Close())    
  44. Application.Run(iqform)  

Simplest input validation using Char.IsNumber and Char.IsLetter

The following example demonstrates how to validate a user input by using the Char.IsNumber and Char.IsLetter character manipulation functions:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. let validateform = new Form(Text="Validate Inputs",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  12. //creates our controls  
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  14. let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(100, 170))   
  15. let label1=new Label(Text="Enter a name:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  16. let nametxtbox=new TextBox(Location=new System.Drawing.Point(120,10),BorderStyle=BorderStyle.FixedSingle)  
  17. validateform.Font<-ffont  
  18. //adds the controls to our form  
  19. validateform.Controls.Add(exitbutton)  
  20. validateform.Controls.Add(okbutton)  
  21. validateform.Controls.Add(label1)  
  22. validateform.Controls.Add(nametxtbox)  
  23. //when the oK button is clicked  
  24. okbutton.Click.Add(fun ok->  
  25. //if the input begins with a letter then  
  26. if (Char.IsLetter(Convert.ToChar(nametxtbox.Text.Chars(0)))) then  
  27. //prompt that its a valid name  
  28. MessageBox.Show("The name you entered is valid ""Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore  
  29. //if the input begins with a number then                           
  30. if (Char.IsNumber(Convert.ToChar(nametxtbox.Text.Chars(0)))) then  
  31. //prompt that its an invalid name  
  32. MessageBox.Show("You must enter a valid name""Validate Inputs",MessageBoxButtons.OK, MessageBoxIcon.Information)|>ignore)  
  33. //when the exit button is clicked  
  34. exitbutton.Click.Add(fun exit->validateform.Close())  
  35. //executes our application  
  36. Application.Run(validateform)  
Click the run icon when done entering these codes in Visual F# code editor window. Try entering any number in the name text box and clicking the ok button. You should see an output similar to the following:

Paint Application in Visual F#

The following example demonstrates how to make a simple Paint-like application in Visual F#:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. open System.Drawing.Drawing2D  
  10. let drawingform = new Form(Text="Draw Objects",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  13. let erasebutton=new Button(Text="Erase", Location=new System.Drawing.Point(120, 200))  
  14. let colorbutton=new Button(Text="Brush Color", Location=new System.Drawing.Point(40, 200))  
  15. drawingform.Controls.Add(exitbutton)  
  16. drawingform.Controls.Add(erasebutton)  
  17. drawingform.Controls.Add(colorbutton)  
  18. //creates a color dialog box  
  19. let colordlg=new ColorDialog()  
  20. //creates a colorblend object  
  21. let mutable color=new ColorBlend()  
  22.   
  23. let gr=drawingform.CreateGraphics()  
  24. gr.SmoothingMode<-SmoothingMode.HighQuality  
  25. //when the form is loaded, change its color to white  
  26. drawingform.Load.Add(fun background->  
  27. //set the default brush color to indigo  
  28. color.Colors<-[|Color.Indigo|]  
  29.                         drawingform.BackColor<-Color.White)  
  30.   
  31. drawingform.MouseMove.Add(fun trail->  
  32. //when the mouse button is moved and the left button is clicked  
  33.   
  34. if (trail.Button=System.Windows.Forms.MouseButtons.Left)then  
  35. //draw the object assign the color seleted from the color dialog as a brush color  
  36. gr.FillRectangle(new SolidBrush(color.Colors.[0]),new Rectangle(trail.X,trail.Y,5,5)))   
  37. //when the erase button is clicked  
  38. //erase the object drawn in the form  
  39. erasebutton.Click.Add(fun erase->gr.Clear(Color.White))   
  40. //when the exit button is clicked  
  41. //quit the form                                                                                                                
  42. exitbutton.Click.Add(fun quit->drawingform.Close())  
  43. //when the brush color button is selected                                                            
  44. colorbutton.Click.Add(fun colors->  
  45. //display the Color Dialog box  
  46. if colordlg.ShowDialog()=DialogResult.OK then  
  47. //store the value selected by the user in our colorblend object  
  48. color.Colors<-[|colordlg.Color|])  
  49. //executes our application  
  50. Application.Run(drawingform)  
Click the run icon once you are done entering these codes in the code editor window. You should see the following outputs:

Mouse Trails using Basic shapes

Obsessed with mouse trails? Try the following sample applications:
1. Line Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.DrawLine(Pens.Peru,trail.X,trail.Y,trail.X+1,trail.Y+1))                                                                                                                  
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

2. Circle Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.DrawEllipse(Pens.IndianRed,new Rectangle(trail.X,trail.Y,10,10)))                                                                                                                      
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

3. Rectangle Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.DrawRectangle(Pens.Fuchsia,new Rectangle(trail.X,trail.Y,5,5)))                                                                                                                      
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

4. Arc Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.DrawArc(Pens.Violet,new Rectangle(trail.X,trail.Y,5,5),180.0f,-180.0f))                                                                                                                 
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

5. Filled-Arc/FillPie Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.FillPie(Brushes.MidnightBlue,new Rectangle(trail.X,trail.Y,10,10),180.0f,180.0f))                                                                                                                 
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

6. Filled Circle Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.FillEllipse(Brushes.Tomato,new Rectangle(trail.X,trail.Y,10,10)))                                                                                                                 
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

7. Filled Rectangle Mouse Trail
Code:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let trailform = new Form(Text="Display Mouse Trail",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let gr=trailform.CreateGraphics()  
  13. trailform.Controls.Add(exitbutton)  
  14. trailform.MouseMove.Add(fun trail->gr.FillRectangle(Brushes.Indigo,new Rectangle(trail.X,trail.Y,20,20)))                                                                                                                 
  15. exitbutton.Click.Add(fun quit->trailform.Close())                                                            
  16. //executes our application  
  17. Application.Run(trailform)  
Output:

Creating a Customized Mouse Pointer

Aside from using pre-defined mouse pointers such as Cursors.Arrow and Cursors.Cross, you can also use customized mouse pointers in Visual F#. Follow these steps for a simple example:

1. Download a cursor creator software such as Axialis CursorWorkShop or Icon Craft.

2. Create a .cur file and name it “pointer.cur”.

3. Start Visual F#>Create a new Project.

4. Enter the following codes:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. let mouseform = new Form(Text="Cutom Mouse Pointer",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  10. //creates our control  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  12. let customcur=new System.Windows.Forms.Cursor("pointer.cur")  
  13. mouseform.MouseHover.Add(fun cutom->mouseform.Cursor<-customcur)   
  14. mouseform.Controls.Add(exitbutton)                                                                                                  
  15. exitbutton.Click.Add(fun quit->mouseform.Close())                                                            
  16. //executes our application  
  17. Application.Run(mouseform)  

5. Click the Open Folder>bin>debug> then copy and paste pointer.cur.

6. Run your application

Printing an Image Data

To print an image or text, use a PrintDocument control. To following example shows how to print an image data, using a PrintDocument control.

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. open System.Drawing.Printing  
  10.   
  11. open System.Drawing.Imaging  
  12. let imageform = new Form(Text="Print Form",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  13. //creates our control  
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  15. let loadbutton=new Button(Text="Load", Location=new System.Drawing.Point(120, 200))  
  16. let prnbutton=new Button(Text="Print", Location=new System.Drawing.Point(40, 200))  
  17. let pic=new PictureBox(SizeMode=PictureBoxSizeMode.StretchImage,Location=new System.Drawing.Point(20, 20),BorderStyle=BorderStyle.FixedSingle,Size=new System.Drawing.Size(100, 100))  
  18. let label=new Label(AutoSize=true,Location=new System.Drawing.Point(0, 120))  
  19. let dlg=new OpenFileDialog()  
  20. let gr=imageform.CreateGraphics()  
  21. let prn=new System.Drawing.Printing.PrintDocument()  
  22. imageform .Controls.Add(pic)  
  23. imageform.Controls.Add(loadbutton)  
  24. imageform.Controls.Add(label)  
  25. imageform.Controls.Add(prnbutton)  
  26. imageform.Controls.Add(exitbutton)  
  27. //sends the data to the printer  
  28. prnbutton.Click.Add(fun startprint->prn.Print())  
  29.   
  30. loadbutton.Click.Add(fun load->  
  31. //filter dialog result  
  32. dlg.Filter <- "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif"  
  33. //adds title to your dialog box  
  34.                    dlg.Title<-"Select an Image File"  
  35.                    if dlg.ShowDialog()=DialogResult.OK then  
  36. //creates a bitmap object  
  37. //assigns the image selected by the user as its value  
  38.                       let bmp=new System.Drawing.Bitmap(dlg.FileName)  
  39.                       bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone)  
  40. //assigns the loaded image as a picturebox value  
  41.                       pic.Image<-bmp  
  42. //displays the image url in our label  
  43.                       label.Text<-"\t\tFilename:" + Convert.ToString(Convert.ToChar(32))+ (dlg.FileName))   
  44. //specifies the data to be printed  
  45. //in this case our image                        
  46. prn.PrintPage.Add(fun printdata->gr.DrawImage(pic.Image,10,10))  
  47. //close the form                                                                                                          
  48. exitbutton.Click.Add(fun quit->imageform.Close())                                           
  49. [<stathread>]                      
  50. //executes our application  
  51. Application.Run(imageform)  
  52. </stathread>  

Drawing Triangles and Circles

To draw a triangle, use the DrawLine or DrawPolygon method. Here’s an example that uses DrawPolygon method:
  1. // Learn more about F# at http://fsharp.net  
  2. // Learn more about F# at http://fsharp.net  
  3. //specifies the memory location of the class files  
  4. //that will be needed in our application  
  5. open System.Collections.Generic  
  6. open System  
  7. open System.Windows.Forms  
  8. open System.ComponentModel  
  9. open System.Drawing  
  10. let graphicform = new Form(Text="Draw Triangle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200))  
  13. graphicform.Paint.Add(fun draw->  
  14. let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|]  
  15. let pen=new Pen(Color.Blue,Width=12.0f)  
  16. draw.Graphics.DrawPolygon(pen,array))  
  17. graphicform.Controls.Add(exitbutton)  
  18. //executes our application  
  19. Application.Run(graphicform)  
To draw a filled triangle, use the FillPolygon method:
  1. // Learn more about F# at http://fsharp.net  
  2. // Learn more about F# at http://fsharp.net  
  3. //specifies the memory location of the class files  
  4. //that will be needed in our application  
  5. open System.Collections.Generic  
  6. open System  
  7. open System.Windows.Forms  
  8. open System.ComponentModel  
  9. open System.Drawing  
  10. let graphicform = new Form(Text="Draw Triangle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200))  
  13. graphicform.Paint.Add(fun draw->  
  14. let array=[|new Point(0,150);new Point(150,10);new Point(300,150)|]  
  15. let brush=new SolidBrush(Color.Blue)  
  16. draw.Graphics.FillPolygon(brush,array))  
  17. graphicform.Controls.Add(exitbutton)  
  18. //executes our application  
  19. Application.Run(graphicform)  
To draw a circle, use the DrawEllipse method:
  1. // Learn more about F# at http://fsharp.net  
  2. // Learn more about F# at http://fsharp.net  
  3. //specifies the memory location of the class files  
  4. //that will be needed in our application  
  5. open System.Collections.Generic  
  6. open System  
  7. open System.Windows.Forms  
  8. open System.ComponentModel  
  9. open System.Drawing  
  10. let graphicform = new Form(Text="Draw Circle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200))  
  13. graphicform.Paint.Add(fun draw->  
  14.   
  15. let pen=new Pen(Color.Blue,Width=12.0f)  
  16. draw.Graphics.DrawEllipse(pen,0.0f,0.0f,100.0f,100.0f))  
  17.   
  18. graphicform.Controls.Add(exitbutton)  
  19. //executes our application  
  20. Application.Run(graphicform)  
To draw a solid circle, use the FillEllipse method:
  1. // Learn more about F# at http://fsharp.net  
  2. // Learn more about F# at http://fsharp.net  
  3. //specifies the memory location of the class files  
  4. //that will be needed in our application  
  5. open System.Collections.Generic  
  6. open System  
  7. open System.Windows.Forms  
  8. open System.ComponentModel  
  9. open System.Drawing  
  10. let graphicform = new Form(Text="Draw Circle",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 250),StartPosition=FormStartPosition.CenterScreen)  
  11. //creates our control  
  12. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 200))  
  13. graphicform.Paint.Add(fun draw->  
  14.   
  15. let brush=new SolidBrush(Color.Blue)  
  16. draw.Graphics.FillEllipse(brush,0.0f,0.0f,100.0f,100.0f))  
  17.   
  18. graphicform.Controls.Add(exitbutton)  
  19. //executes our application  
  20. Application.Run(graphicform)  

Display the Date or Time from a DateTime Object

There are several ways on how to retrieve the date or time part from a datetime object, here are the few methods that I discover through research and experimentation:

1. Using Substring

Display the time part using Substring:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  12.   
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  14. //create a timer object and set its interval to 1 second  
  15. //by default timer are disabled so you'll need to enable it  
  16. let timer1=new Timer(Interval=1000,Enabled=true)  
  17. timelabel.Font<-ffont  
  18. //change it every 1 min second  
  19. timer1.Tick.Add(fun time->  
  20. //assigns the current date and time to a variable  
  21. let datetime=Convert.ToString(System.DateTime.Now)  
  22. //retrieves the value of the datetime variable  
  23. //starting from the 11th character  
  24. let timepart=datetime.Substring(10)  
  25. //display the time  
  26. timelabel.Text<-timepart)  
  27.                   
  28. //adds the exit button to our form  
  29. timerform.Controls.Add(timelabel)  
  30. timerform.Controls.Add(exitbutton)  
  31. //when the exit button is clicked  
  32. exitbutton.Click.Add(fun quit->  
  33. //stops the time  
  34. timer1.Stop()  
  35. //close the form  
  36. timerform.Close())                     
  37. //show our form  
  38. timerform.Show()  
  39. //execute our application  
  40. Application.Run(timerform)  

Display the time part using the substring function:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //use the random function to generate random numbers  
  12. let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  13.   
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  15.   
  16. datelabel.Font<-ffont  
  17. dateform.Load.Add(fun time->  
  18. //assigns the current date and time to the datetime variable  
  19. let datetime=Convert.ToString(System.DateTime.Now)  
  20. //retrieves the text from the datetime variable staring from the first  
  21. //character to the 11th character  
  22. let datepart=datetime.Substring(0,10)  
  23. //display the current time  
  24. datelabel.Text<-datepart)  
  25.                   
  26. //adds the exit button to our form  
  27. dateform.Controls.Add(datelabel)  
  28. dateform.Controls.Add(exitbutton)  
  29. //when the exit button is clicked  
  30. exitbutton.Click.Add(fun quit->dateform.Close())                     
  31. //execute our application  
  32. Application.Run(dateform)  


2. Using Remove

Display the time part using Remove:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let timerform=new Form(Text="Display Time Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let timelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  12.   
  13. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  14. //create a timer object and set its interval to 1 second  
  15. //by default timer are disabled so you'll need to enable it  
  16. let timer1=new Timer(Interval=1000,Enabled=true)  
  17. timelabel.Font<-ffont  
  18. //change it every 1 min second  
  19. timer1.Tick.Add(fun time->  
  20. //assigns the current date and time to the datetime variable  
  21. let datetime=Convert.ToString(System.DateTime.Now)  
  22. //removes the first 11 characters from the datetime variable  
  23. let timepart=datetime.Remove(0,10)  
  24. //display the current time  
  25. timelabel.Text<-timepart)  
  26.                   
  27. //adds the exit button to our form  
  28. timerform.Controls.Add(timelabel)  
  29. timerform.Controls.Add(exitbutton)  
  30. //when the exit button is clicked  
  31. exitbutton.Click.Add(fun quit->  
  32. //stops the time  
  33. timer1.Stop()  
  34. //close the form  
  35. timerform.Close())                     
  36. //show our form  
  37. timerform.Show()  
  38. //execute our application  
  39. Application.Run(timerform)  

Display the date part using the Remove function:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use this to enable the intellisense. Very helpful in coding your application  
  5. open System.Drawing   
  6. //specify the location of the Form classes  
  7. open System.Windows.Forms  
  8. let ffont=new Font("Verdana", 14.5F,FontStyle.Regular, GraphicsUnit.Point)  
  9. //creates a form  
  10. let dateform=new Form(Text="Display Date Part",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //use the random function to generate random numbers  
  12. let datelabel=new Label(Location=new System.Drawing.Point(20,40),BorderStyle=BorderStyle.FixedSingle,AutoSize=true)  
  13.   
  14. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220),AutoSize=true)  
  15.   
  16. datelabel.Font<-ffont  
  17. dateform.Load.Add(fun time->  
  18. //assigns the current date and time to the datetime variable  
  19. let datetime=Convert.ToString(System.DateTime.Now)  
  20. //removes the text from the datetime variable staring from the 11 character  
  21. let datepart=datetime.Remove(10)  
  22. //display the current time  
  23. datelabel.Text<-datepart)  
  24.                   
  25. //adds the exit button to our form  
  26. dateform.Controls.Add(datelabel)  
  27. dateform.Controls.Add(exitbutton)  
  28. //when the exit button is clicked  
  29. exitbutton.Click.Add(fun quit->dateform.Close())                     
  30. //execute our application  
  31. Application.Run(dateform)  

SystemSounds in F#

To play a system sound in Visual F#, use the SystemSounds object. For a simple example on using the SystemSounds object, try the following:


  1. // Learn more about F# at http://fsharp.net  
  2. //use the f# standard library  
  3. open System  
  4. //use media classes  
  5. open System.Media  
  6. //specify the memory location of the classes used in drawing objects  
  7. //required to draw the listbox item text  
  8. open System.Drawing  
  9. //specify the location of the form class  
  10. open System.Windows.Forms  
  11. //creates a form and assign a "Play System Sounds" caption to it  
  12. let soundform=new Form(Text="Play System Sounds",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  13. //creates a label and set its Text to “Count”  
  14. let lbl=new Label(Text="System sounds:", Location=new System.Drawing.Point(20,10),AutoSize=true)  
  15. //makes a listbox  
  16. let soundlistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)  
  17. //adds an item to the listbox when the form is loaded  
  18. soundform.Load.Add(fun items->  
  19.      //adds the items and ignore the passed index position values  
  20.                     soundlistbox.Items.Add("Asterisk")|>ignore  
  21.                     soundlistbox.Items.Add("Beep")|>ignore  
  22.                     soundlistbox.Items.Add("Exclaimation")|>ignore  
  23.                     soundlistbox.Items.Add("Hand")|>ignore  
  24.                     soundlistbox.Items.Add("Question")|>ignore)  
  25. soundlistbox.Click.Add(fun playsound->  
  26.                     if soundlistbox.SelectedIndex=0 then  
  27.                         SystemSounds.Asterisk.Play()  
  28.                         MessageBox.Show("Asterisk""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)|>ignore  
  29.                     if soundlistbox.SelectedIndex=1 then  
  30.                         SystemSounds.Beep.Play()  
  31.                         MessageBox.Show("Beep""System Sounds", MessageBoxButtons.OK)|>ignore  
  32.                     if soundlistbox.SelectedIndex=2 then  
  33.                         SystemSounds.Exclamation.Play()  
  34.                         MessageBox.Show("Exclaimation""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)|>ignore  
  35.                     if soundlistbox.SelectedIndex=3 then  
  36.                         SystemSounds.Hand.Play()  
  37.                         MessageBox.Show("Hand""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Hand)|>ignore  
  38.                     if soundlistbox.SelectedIndex=4 then  
  39.                        SystemSounds.Question.Play()  
  40.                         MessageBox.Show("Question""System Sounds", MessageBoxButtons.OK, MessageBoxIcon.Question)|>ignore)   
  41.                                              
  42. //displays the label to our form  
  43. soundform.Controls.Add(lbl)        
  44. //adds the listbox to our form      
  45. soundform.Controls.Add(soundlistbox)             
  46. soundform.Show()  
  47. Application.Run(soundform)  

This will generate the following output: