Adding space to a concatenated String

There are several ways to add space to a contanated string in Visual F#:

1.Using the Tab Escape Character

Syntax:
  1. “text1” + “\t” + “textn”  

For instance:
  1. Trace.WriteLine("Adding" +”\t”+ “Space”)  

This will display “Adding Space” with 5-6 spaces in between in the output window. You can also use the unicode equivalent of tab which is \u0009. For example:
  1. Trace.WriteLine("Adding" +”\u0009”+ “Space”)  
2. Typing a space or “ “ between the concatenated strings
Syntax:
  1. “text1” + “ ” + “textn”  

For instance:
  1. Trace.WriteLine("Adding" +” ”+ “Space”)  
You can also use the unicode equivalent of space which is \u0032. For example:
  1. Trace.WriteLine("Adding" +”\u0032”+ “Space”)  
3. Using the Convert.ToChar() string manipulation function
Convert.ToChar converts a specified key code to character.
The key code for tab is 9 while Spacebar has 32.

Syntax:
  1. Convert.ToChar(key code)  
Examples:
  1. //backspace key  
  2. Trace.WriteLine(Convert.ToString(Convert.ToChar(8)))  
  3. //enter key  
  4. Trace.WriteLine(Convert.ToString(Convert.ToChar(13)))  
  5. //adds space to a concatenated string  
  6. Trace.WriteLine(“Adding” + Convert.ToString(Convert.ToChar(32)) + “Space” )  

For a simple example of application that adds space to a concatenated string:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Diagnostics  
  6. open System.Windows.Forms  
  7. open System.Drawing  
  8. //creates our form  
  9. let myform= new Form(Text="Adding Space")  
  10. //creates our controls    
  11. let label1=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  12. let label2=new Label(Text="Lastname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  13. let fnametextbox=new TextBox(Location=new System.Drawing.Point(100,10),BorderStyle=BorderStyle.FixedSingle)  
  14. let lnametextbox=new TextBox(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  15. let okbutton=new Button(Text="Ok", Location=new System.Drawing.Point(120, 170))  
  16. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))  
  17. myform.Controls.Add(label1)  
  18. myform.Controls.Add(label2  
  19. myform.Controls.Add(fnametextbox)  
  20. myform.Controls.Add(lnametextbox)  
  21. myform.Controls.Add(okbutton)  
  22. myform.Controls.Add(exitbutton)  
  23. myform.Click.Add(fun space->  
  24. //display our text in the ouput window  
  25. Trace.WriteLine("Adding Spaces")  
  26. Trace.WriteLine(label1.Text + "\u0009" + fnametextbox.Text)  
  27. Trace.WriteLine(label2.Text + "\u0009" + lnametextbox.Text))  
  28.   
  29. //execute our application  
  30. myform.Show()  
  31. Application.Run(myform)  

Gradient and Textured Form in Visual F#

To create a gradient form in Visual F#, simply use a gradient brush and use that brush to draw a rectangle that will cover the whole form area. To create a LinearGradientBrush, use the following syntax:
  1. let newbrush=new LinearGradientBrush(brushpoint,color1,color2,strokeangle)  
For instance:
  1. let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)  
Follow these steps to create a gradient form using the LinearGradientBrush drawing method:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Drawing  
  7. open System.Drawing.Drawing2D  
  8. //creates our form  
  9. let myform= new Form(Text="Add Gradient Background")  
  10. //create a rectangle  
  11. //change its upper left x and y coordinates to 0,0  
  12. //sets it width and height to the width and height of the form  
  13. let rect=new Rectangle(0,0,myform.Width,myform.Height)  
  14. //creates a gradient brush  
  15. let newbrush=new LinearGradientBrush(rect,Color.Green,Color.YellowGreen,45.0F)  
  16. //paints our form with a solid and filled rectangle  
  17. myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush,rect))   
  18. //executes our application  
  19. myform.Show()  
  20. Application.Run(myform)  

This will display the following output:



To create a textured form, use a TextureBrush to draw a rectangle that will cover the whole form area then display that rectangle on your form.

For instance:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the namespace memory location of the classes that  
  3. //will be used in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Drawing  
  7. open System.Drawing.Drawing2D  
  8.   
  9. //creates our form  
  10. let myform= new Form(Text="Add Textured Background")  
  11. //draws a rectangle with the same width and height as our form  
  12. let drawrect=new Rectangle(0,0,myform.Width,myform.Height)  
  13. //creates a texturedbrush using a windows default image  
  14. //tile the image Vertically and Horizontally  
  15. let newbrush=new TextureBrush(new Bitmap("C:\Windows\Zapotec.bmp"),WrapMode=WrapMode.TileFlipXY)  
  16. //applies the brush in drawing our rectangle  
  17. myform.Paint.Add(fun fillrect->fillrect.Graphics.FillRectangle(newbrush, drawrect))   
  18. //executes our application  
  19. myform.Show()  
  20. Application.Run(myform)  

Add navigational buttons to a database application without using a BindingNavigator

On my previous post, we were able to add a navigational button to our application by using a BindingNavigator, today, we will do everything manually. To add a navigational button by hand, all you need to do is to bind your controls using this method and increment or decrement the Row index number to navigate around your records. This method is slightly similar to the methods used by most developers in adding navigational buttons to VB.net database applications.

For the sake of example, follow these steps(I assume here that you have already created a database file named “dbEmployee” and a table named “tblEmployee”):

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. open System.Data.OleDb  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. //creates a connection object  
  12. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  13.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  14.  //creates an OleDbDataAdapter  
  15. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  16. let deletecommand=new System.Data.OleDb.OleDbCommand()  
  17.   
  18. //generates a dataset  
  19. let dataset11 = new DataSet()  
  20. //fills the dataset with recod values  
  21. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  22. //creates a form  
  23. let dataform = new Form(Text="Add Navigational Buttons Manually",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  24. //creates our controls  
  25. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 170))    
  26. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  27. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  28. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  29. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  30. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  31. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  32. let topbutton=new Button(Text="Top", Location=new System.Drawing.Point(40, 170))  
  33. let bottombutton=new Button(Text="Bottom", Location=new System.Drawing.Point(120, 170))  
  34. //assings the font to our form  
  35. dataform.Font<-ffont  
  36. //adds the controls to our form  
  37. dataform.Controls.Add(exitbutton)  
  38. dataform.Controls.Add(label1)  
  39. dataform.Controls.Add(label2)  
  40. dataform.Controls.Add(label3)  
  41. dataform.Controls.Add(emplabel)  
  42. dataform.Controls.Add(fnamelabel)  
  43. dataform.Controls.Add(lnamelabel)  
  44. dataform.Controls.Add(topbutton)  
  45. dataform.Controls.Add(bottombutton)  
  46.   
  47. //binds the fieldnames to our label  
  48. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  49. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  50. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))  
  51.   
  52. //when the topbutton is clicked  
  53. //assigns 0 rowindexnumber value causing the   
  54. //first record to be displayed  
  55. topbutton.Click.Add(fun top->  
  56. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  57. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  58. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2)))  
  59.   
  60. //when the bottombutton is clicked  
  61. //assign the index number of the last row as a rowindexnumer  
  62. //since we only have two records on our table  
  63. //so the index number of the last row is understood to be 1  
  64. //when manipulating large number of records, i suggest using  
  65. //dataset11.Tables.["tblEmployee"].Rows.Count() to  
  66. //automatically count the number of records  
  67.   
  68. bottombutton.Click.Add(fun bottom->  
  69. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(0))  
  70. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(1))  
  71. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(1).Item(2)))  
  72.   
  73. //when the exit button is clicked  
  74. exitbutton.Click.Add(fun exit->  
  75. //close the form and dataconnection  
  76.                     dataform.Close()  
  77.                     oleconn.Close())  
  78.   
  79. //executes our application  
  80. dataform.Show()  
  81. Application.Run(dataform)  

5. This will display the following output:



6. Simple...right?

Yet another way of binding a control to a field name in Visual F#

On my last post, I’ve shown you how to bind a record value to a control using the DataBindings method. Another way of binding a control to a fieldname is by using Text property of a control and assigning the dataset collection properties as its value:

Syntax:

  1. controlobjvariable.Text<- (datasetname.Tables.["tablename"].Rows.Item(rowindexnumber).Item(columnindexnumber))  
For instance:
  1. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
The rowindexnumber of the first record on your table is 0, the second record is 1 and so on. Same goes with the columnindexnumber. For a simple example of application that uses the binding method above, follow these steps(I assume here that you have already created a database file named “dbEmployee” and a table named “tblEmployee”):
1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.
2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.
3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.
4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. open System.Data.OleDb  
  9. //creates a font  
  10. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  11. //creates a connection object  
  12. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  13.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  14.  //creates an OleDbDataAdapter  
  15. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  16. let deletecommand=new System.Data.OleDb.OleDbCommand()  
  17.   
  18. //generates a dataset  
  19. let dataset11 = new DataSet()  
  20. //fills the dataset with recod values  
  21. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  22. //creates a form  
  23. let dataform = new Form(Text="Bind Form Control Part 2",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  24. //creates our controls  
  25. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  26. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  27. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  28. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  29. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  30. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  31. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  32. //assings the font to our form  
  33. dataform.Font<-ffont  
  34. //adds the controls to our form  
  35. dataform.Controls.Add(exitbutton)  
  36. dataform.Controls.Add(label1)  
  37. dataform.Controls.Add(label2)  
  38. dataform.Controls.Add(label3)  
  39. dataform.Controls.Add(emplabel)  
  40. dataform.Controls.Add(fnamelabel)  
  41. dataform.Controls.Add(lnamelabel)  
  42.   
  43. //binds the fieldnames to our label  
  44. emplabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(0))  
  45. fnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(1))  
  46. lnamelabel.Text<-Convert.ToString(dataset11.Tables.["tblEmployee"].Rows.Item(0).Item(2))  
  47.   
  48. //when the exit button is clicked  
  49. exitbutton.Click.Add(fun exit->  
  50. //close the form and dataconnection  
  51.                     dataform.Close()  
  52.                     oleconn.Close())  
  53.   
  54. //executes our application  
  55. dataform.Show()  
  56. Application.Run(dataform)  
5. This will display the following output:

Manipulating an Image Data in Visual F#

To manipulate an image data, simply add an oleobject fieldname on your table and insert a bitmap image to it. To display the image on your form, use a picturebox control then bind your oleobject fieldname to it using the following syntax:
  1. Pictureboxobjname.DataBindings.Add(new Binding(pictureboxproperty,datasource,fieldname))  
For instance:
  1. appico.DataBindings.Add(new Binding("Image",bindingsource,"oleapplogo",true))  
The last boolean parameter enables the control formatting. We also use the Image property instead of the usual Text for the fact that it is the property used to display an image on your form.

Before proceding to the steps below, I want you to make an Ms-Access 2003 or 2007 database file named “dbApplication”. Create a table inside it and name it “tblApplication”. Use the following specifications:



Chrappname Text Handles the application name
Oleapplogo OLE Object Handles the application ico

After creating your table, you can add appropriate values to it for instance:




chrappname oleapplogo
PHP Bitmap Image
Apache Bitmap Image

Bitmap Image refers the the Bitmap Object that you have inserted using the Paste From Ms-Paint command.

After creating the table and adding appropriate values to it.We are now ready for our sample application. Just follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a font  
  9. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  10. //creates a connection object  
  11. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  12.   Data Source=C:\Documents and Settings\station 2\My Documents\dbApplication.mdb")  
  13.  //creates an OleDbDataAdapter  
  14. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblApplication", oleconn)  
  15. //generates a dataset  
  16. let dataset11 = new DataSet()  
  17. //fills the dataset with recod values  
  18. dataadpter.Fill(dataset11,"tblApplication")|>ignore  
  19. //creates a form  
  20. let dataform = new Form(Text="Manipulate Image Data",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  21. //creates our controls    
  22. let label1=new Label(Text="App. name:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  23. let label2=new Label(Text="Icon:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  24. let appnamelabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  25. let appico=new PictureBox(SizeMode=PictureBoxSizeMode.StretchImage,Location=new System.Drawing.Point(140,50))  
  26.   
  27. let bindingsource=new BindingSource()  
  28. //creates a binding navigator  
  29. //this will allow us to add navigational buttons to our data grid  
  30. let bindingnav=new BindingNavigator(Dock=DockStyle.None,Location=new System.Drawing.Point(100, 170))  
  31. //creates a toolstrip buttons for our binding navigator  
  32. let movefirst=new ToolStripButton(Text="Top")  
  33. let moveprev=new ToolStripButton(Text="Prev")  
  34. let movenext=new ToolStripButton(Text="Next")  
  35. let movelast=new ToolStripButton(Text="Bottom")  
  36. let exitbutton=new ToolStripButton(Text="Exit")  
  37. //adds the toolstripbuttons to our binding navigator  
  38. bindingnav.Items.Add(movefirst)|>ignore  
  39. bindingnav.Items.Add(moveprev)|>ignore  
  40. bindingnav.Items.Add(movenext)|>ignore  
  41. bindingnav.Items.Add(movelast)|>ignore  
  42. bindingnav.Items.Add(exitbutton)|>ignore  
  43. //adds a function to each buttons  
  44. bindingnav.MoveFirstItem<-movefirst  
  45. bindingnav.MoveNextItem<-movenext  
  46. bindingnav.MovePreviousItem<-moveprev  
  47. bindingnav.MoveLastItem<-movelast  
  48. exitbutton.Click.Add(fun exit->  
  49. //close the form and dataconnection  
  50.                     dataform.Close()  
  51.                     oleconn.Close())  
  52. //assigns the dataset name as a bindingsource datasource  
  53. bindingsource.DataSource<-dataset11  
  54. //assigns our table as a binding source datamember  
  55. bindingsource.DataMember<-"tblApplication"  
  56. //assigns the bindingsource name as a binding navigators  
  57. //bindingsource value  
  58. bindingnav.BindingSource<-bindingsource  
  59. //opens the connection  
  60. oleconn.Open()  
  61. //assings the font to our form  
  62. dataform.Font<-ffont  
  63. //adds the controls to our form  
  64. dataform.Controls.Add(label1)  
  65. dataform.Controls.Add(label2)  
  66. dataform.Controls.Add(appnamelabel)  
  67. dataform.Controls.Add(appico)  
  68.   
  69. dataform.Controls.Add(bindingnav)  
  70. //binds the fieldnames to our label  
  71. appnamelabel.DataBindings.Add(new Binding("Text",bindingsource,"chrappname"))  
  72. appico.DataBindings.Add(new Binding("Image",bindingsource,"oleapplogo",true))  
  73. //executes our application  
  74. dataform.Show()  
  75. Application.Run(dataform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: 6. Here’s another version of the code above without BindingSource and BindingNavigator.
  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a font  
  9. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  10. //creates a connection object  
  11. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  12.   Data Source=C:\Documents and Settings\station 2\My Documents\dbApplication.mdb")  
  13.  //creates an OleDbDataAdapter  
  14. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblApplication", oleconn)  
  15. //generates a dataset  
  16. let dataset11 = new DataSet()  
  17. //fills the dataset with recod values  
  18. dataadpter.Fill(dataset11,"tblApplication")|>ignore  
  19. //creates a form  
  20. let dataform = new Form(Text="Manipulate Image Data",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  21. //creates our controls    
  22. let label1=new Label(Text="App. name:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  23. let label2=new Label(Text="Icon:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  24. let appnamelabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  25. let appico=new PictureBox(SizeMode=PictureBoxSizeMode.StretchImage,Location=new System.Drawing.Point(140,50))  
  26. //opens the connection  
  27. oleconn.Open()  
  28. //assings the font to our form  
  29. dataform.Font<-ffont  
  30. //adds the controls to our form  
  31. dataform.Controls.Add(label1)  
  32. dataform.Controls.Add(label2)  
  33. dataform.Controls.Add(appnamelabel)  
  34. dataform.Controls.Add(appico)  
  35. //binds the fieldnames to our label  
  36. appnamelabel.DataBindings.Add(new Binding("Text",dataset11,"tblApplication.chrappname"))  
  37. appico.DataBindings.Add(new Binding("Image",dataset11,"tblApplication.oleapplogo",true))  
  38. //executes our application  
  39. dataform.Show()  
  40. Application.Run(dataform)  
For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.

Adding navigational Buttons to your database application

The fastest way to add navigational buttons to your database application is to use a BindingNavigator. If you are not yet familiar with Binding Navigator, I suggest reading this post first before proceeding to the steps below:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a font  
  9. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  10. //creates a connection object  
  11. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  12.   Data Source=C:\Documents and Settings\station 2\My Documents\dbEmployee.mdb")  
  13.  //creates an OleDbDataAdapter  
  14. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  15. //generates a dataset  
  16. let dataset11 = new DataSet()  
  17. //fills the dataset with recod values  
  18. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  19. //creates a form  
  20. let dataform = new Form(Text="Add Navigational Buttons",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  21. //creates our controls    
  22. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  23. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  24. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  25. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  26. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  27. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  28. let bindingsource=new BindingSource()  
  29. //creates a binding navigator  
  30. //this will allow us to add navigational buttons to our data grid  
  31. let bindingnav=new BindingNavigator(Dock=DockStyle.None,Location=new System.Drawing.Point(100, 170))  
  32. //creates a toolstrip buttons for our binding navigator  
  33. let movefirst=new ToolStripButton(Text="Top")  
  34. let moveprev=new ToolStripButton(Text="Prev")  
  35. let movenext=new ToolStripButton(Text="Next")  
  36. let movelast=new ToolStripButton(Text="Bottom")  
  37. let exitbutton=new ToolStripButton(Text="Exit")  
  38. //adds the toolstripbuttons to our binding navigator  
  39. bindingnav.Items.Add(movefirst)|>ignore  
  40. bindingnav.Items.Add(moveprev)|>ignore  
  41. bindingnav.Items.Add(movenext)|>ignore  
  42. bindingnav.Items.Add(movelast)|>ignore  
  43. bindingnav.Items.Add(exitbutton)|>ignore  
  44. //adds a function to each buttons  
  45. bindingnav.MoveFirstItem<-movefirst  
  46. bindingnav.MoveNextItem<-movenext  
  47. bindingnav.MovePreviousItem<-moveprev  
  48. bindingnav.MoveLastItem<-movelast  
  49. exitbutton.Click.Add(fun exit->  
  50. //close the form and dataconnection  
  51.                     dataform.Close()  
  52.                     oleconn.Close())  
  53. //assigns the dataset name as a bindingsource datasource  
  54. bindingsource.DataSource<-dataset11  
  55. //assigns our table as a binding source datamember  
  56. bindingsource.DataMember<-"tblEmployee"  
  57. //assigns the bindingsource name as a binding navigators  
  58. //bindingsource value  
  59. bindingnav.BindingSource<-bindingsource  
  60. //opens the connection  
  61. oleconn.Open()  
  62. //assings the font to our form  
  63. dataform.Font<-ffont  
  64. //adds the controls to our form  
  65. dataform.Controls.Add(label1)  
  66. dataform.Controls.Add(label2)  
  67. dataform.Controls.Add(label3)  
  68. dataform.Controls.Add(emplabel)  
  69. dataform.Controls.Add(fnamelabel)  
  70. dataform.Controls.Add(lnamelabel)  
  71. dataform.Controls.Add(bindingnav)  
  72. //binds the fieldnames to our label  
  73. emplabel.DataBindings.Add(new Binding("Text",bindingsource,"chrempno"))  
  74. fnamelabel.DataBindings.Add(new Binding("Text",bindingsource,"chrfname"))  
  75. lnamelabel.DataBindings.Add(new Binding("Text",bindingsource,"chrlname"))  
  76. //executes our application  
  77. dataform.Show()  
  78. Application.Run(dataform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.

BindingSource and BindingNavigator(OleDbDataAdapter Visual F#)

BindingSource allows us to simplify the binding process to our control. To create a binding source in Visual F#, use the following syntax:
  1. let bindingsourceobjname=new BindingSource()  
for instance:
  1. let bindingsource=new BindingSource()  
The two most important properties of BindingSource component are DataSource which specifies the data source of the bindingsource, and the Datamember which specifies the specific table to which your bindingsource is bound to.

BindingNavigator are simply used to add navigational buttons to your database application. The syntax in creating a binding source is:
  1. let bindingnavigatorobjvariable=new BindingNavigator()  
for instance:
  1. let bindingnav=new BindingNavigator()  
One of the essential properties of the BindingNavigator component is the BindingSource which specifies the bindingsource name to which the BindingNavigator is bound to.

Follow these steps for a simple example on using binding navigator:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the location of the class files  
  3. //that will be needed in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a connection object  
  9. let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  10.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  11. //adds an oleDbDataAdapter  
  12. let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)  
  13. //generate a dataset  
  14. let dataset11 = new DataSet()  
  15. //fill our dataset with record values  
  16. dataadapter.Fill(dataset11,"tblEmployee")|>ignore  
  17. //creates a form  
  18. let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))  
  19. //creates a datagrid  
  20. let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))  
  21. //creates a binding source to simplify the binding process  
  22. let bindingsource=new BindingSource()  
  23. //creates a binding navigator  
  24. //this will allow us to add navigational buttons to our data grid  
  25. let bindingnav=new BindingNavigator()  
  26. //creates a toolstrip buttons for our binding navigator  
  27. let movefirst=new ToolStripButton(Text="Top")  
  28. let moveprev=new ToolStripButton(Text="Prev")  
  29. let movenext=new ToolStripButton(Text="Next")  
  30. let movelast=new ToolStripButton(Text="Bottom")  
  31. //adds the toolstripbuttons to our binding navigator  
  32. bindingnav.Items.Add(movefirst)|>ignore  
  33. bindingnav.Items.Add(moveprev)|>ignore  
  34. bindingnav.Items.Add(movenext)|>ignore  
  35. bindingnav.Items.Add(movelast)|>ignore  
  36. //adds a function to each buttons  
  37. bindingnav.MoveFirstItem<-movefirst  
  38. bindingnav.MoveNextItem<-movenext  
  39. bindingnav.MovePreviousItem<-moveprev  
  40. bindingnav.MoveLastItem<-movelast  
  41. //assigns the dataset name as a bindingsource datasource  
  42. bindingsource.DataSource<-dataset11  
  43. //assigns our table as a binding source datamember  
  44. bindingsource.DataMember<-"tblEmployee"  
  45. //assigns the bindingsource name as a binding navigators  
  46. //bindingsource value  
  47. bindingnav.BindingSource<-bindingsource  
  48. //opens the connection  
  49. olecon.Open()  
  50. //adds the controls to our form  
  51. dataform.Controls.Add(bindingnav)  
  52. datagrid.DataSource <- bindingsource  
  53.   
  54. //add the datagrid to our form  
  55. dataform.Controls.Add(datagrid)  
  56. //execute our application  
  57. dataform.Show()  
  58. Application.Run(dataform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.

Changing the DataGridView Header Text in Visual F#

To change the header text of a grid contrl in Visual F#, the first thing that you’ll need to do is to create a grid view columns. This can be done by using the following syntax:
  1. let columnname=new DataGridViewTextBoxColumn()  
For instance:
  1. let chrempnocol=new DataGridViewTextBoxColumn()  
After that, you’ll need to add that column to the data grid. This can be done by using the following syntax:
  1. dataobjvariable.Columns.Add(columnname)  
For example:
  1. datagrid.Columns.Add(chrempnocol)  
Then finally, you’ll need to bind the column name to the fieldname where you will be getting the record value followed by changing its header text using the DataPropertyName and HeaderText propeties.

For a simple example on changing the header text of a grid control, follow these steps:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
1John Doe
2Jean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the location of the class files  
  3. //that will be needed in our application  
  4. open System  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a connection object  
  9. let olecon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  10.   Data Source=C:\Documents and Settings\Administrator\My Documents\dbEmployee.mdb")  
  11. //adds an oleDbDataAdapter  
  12. let dataadapter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", olecon)  
  13. //generate a dataset  
  14. let dataset11 = new DataSet()  
  15. //fill our dataset with record values  
  16. dataadapter.Fill(dataset11,"tblEmployee")|>ignore  
  17. //creates a form  
  18. let dataform = new Form(Text="Change DataGrid Header",ClientSize=new System.Drawing.Size(398, 232))  
  19. //creates a datagrid  
  20. let datagrid = new DataGridView(ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize,Size=new System.Drawing.Size(341, 143),Location=new System.Drawing.Point(22, 38))  
  21. //creates a grid control colums  
  22. let chrempnocol=new DataGridViewTextBoxColumn()  
  23. let chrfnamecol=new DataGridViewTextBoxColumn()  
  24. let chrlnamecol=new DataGridViewTextBoxColumn()  
  25. //adds the columns into our datagrid  
  26. datagrid.Columns.Add(chrempnocol)|>ignore  
  27. datagrid.Columns.Add(chrfnamecol)|>ignore  
  28. datagrid.Columns.Add(chrlnamecol)|>ignore  
  29. //opens the database connection and  
  30. //set our table as the datagrid's datasource  
  31. olecon.Open()  
  32. datagrid.DataSource <- dataset11.Tables.["tblEmployee"]  
  33. //links our fieldname to each grid  
  34. //and change its header text  
  35. chrempnocol.DataPropertyName<-"chrempno"  
  36. chrempnocol.HeaderText<-"Employee No."  
  37. chrfnamecol.DataPropertyName<-"chrfname"  
  38. chrfnamecol.HeaderText<-"First Name"  
  39. chrlnamecol.DataPropertyName<-"chrlname"  
  40. chrlnamecol.HeaderText<-"Last Name"  
  41. //add the datagrid to our form  
  42. dataform.Controls.Add(datagrid)  
  43. //execute our application  
  44. dataform.Show()  
  45. Application.Run(dataform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:
For more sketchy tutorials on Visual F# visit Microsoft F# Developer Center.That's all for now:)

Binding a Record Value To a Control (OleDbDataAdapter Visual F#)

To bind a record value to a control, simply use the DataBindings method of the control which has the following syntax:

  1. controlobjvariable.DataBindings.Add(new Binding(controlproperty, datasetname, fieldname))  
For instance:

  1. fnamelabel.DataBindings.Add(new Binding("Text", dataset11, "Table1.fname"true))  
For a simple data binding example, follow these steps:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
2010AJohn Doe
2010BJean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a font  
  9. let ffont=new Font("Verdana", 9.75F,FontStyle.Regular, GraphicsUnit.Point)    
  10. //creates a connection object  
  11. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  12.   Data Source=C:\Documents and Settings\station 2\My Documents\dbEmployee.mdb")  
  13.  //creates an OleDbDataAdapter  
  14. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  15. //generates a dataset  
  16. let dataset11 = new DataSet()  
  17. //fills the dataset with recod values  
  18. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  19. //creates a form  
  20. let dataform = new Form(Text="Use OleDbDataAdapter",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  21. //creates our controls  
  22. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  23. let label1=new Label(Text="Employee number:",Location=new System.Drawing.Point(0, 10),AutoSize=true)  
  24. let label2=new Label(Text="Firstname:",Location=new System.Drawing.Point(0, 50),AutoSize=true)  
  25. let label3=new Label(Text="Lastname:",Location=new System.Drawing.Point(0,100),AutoSize=true)  
  26. let emplabel=new Label(Location=new System.Drawing.Point(140,10),BorderStyle=BorderStyle.FixedSingle)  
  27. let fnamelabel=new Label(Location=new System.Drawing.Point(100,50),BorderStyle=BorderStyle.FixedSingle)  
  28. let lnamelabel=new Label(Location=new System.Drawing.Point(100,100),BorderStyle=BorderStyle.FixedSingle)  
  29. //assings the font to our form  
  30. dataform.Font<-ffont  
  31. //adds the controls to our form  
  32. dataform.Controls.Add(exitbutton)  
  33. dataform.Controls.Add(label1)  
  34. dataform.Controls.Add(label2)  
  35. dataform.Controls.Add(label3)  
  36. dataform.Controls.Add(emplabel)  
  37. dataform.Controls.Add(fnamelabel)  
  38. dataform.Controls.Add(lnamelabel)  
  39. //binds the fieldnames to our label  
  40. emplabel.DataBindings.Add(new Binding("Text",dataset11,"tblEmployee.chrempno"))  
  41. fnamelabel.DataBindings.Add(new Binding("Text",dataset11,"tblEmployee.chrfname"))  
  42. lnamelabel.DataBindings.Add(new Binding("Text",dataset11,"tblEmployee.chrlname"))  
  43. //when the exit button is clicked  
  44. exitbutton.Click.Add(fun exit->  
  45. //close the form and dataconnection  
  46.                     dataform.Close()  
  47.                     oleconn.Close())  
  48.   
  49. //executes our application  
  50. dataform.Show()  
  51. Application.Run(dataform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

Linking to an Ms-Access DataSource using OledbDataAdapter in Visual F#

To link to an Ms-Access data source, the first thing that you’ll need to do is to create an object variable based on the OleDbConnection class. OleDbConnection specifies the name of the Data Provider and the Location of the database file that you wish to link to. To create an OleDbConnection object use the following syntax:
  1. let Oledbconnectionobjvariable = new System.Data.OleDb.OleDbConnection("Provider=DataProviderName;  
  2. Data Source=Url and filename of the database file")  
For instance:
  1. let Oledbconnectionobjvariable = new System.Data.OleDb.OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;  
  2. Data Source=C:\Mydatabasefile.mdb")  
Microsoft.Jet.OLEDB.4.0 is a data provider used to link to Ms-Access 2003 or higher database files.
After creating an OledbConnectionObject, the next thing that you’ll need to do is to add an OleDbDataAdapter control, to specify how the data will be displayed on your form and the information about the connection. This can be done by using the following syntax:
  1. let oledbdataadapterobjvariable= new System.Data.OleDb. OleDbDataAdapter("SQL statement", ConnectionString)  
ConnectionString refers to the information about the connection and is saved in the Oledbconnection object.

Lastly, youll need to create a storage location for your table values. This can be done by creating a Dataset. To create a dataset, use the following syntax:
  1. Let Datasetobjvariable=new Dataset()  
For a simple example on using OleDbDataAdapter to an AccessDataSource, follow these steps:

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:

Field NameData Type Description
chrempno text Handles employee id
chrfnametext Handles employee’s name
chrlname text Holds employee’s last name


After designing the structure of your table, you can enter appropriate values for each field. For instance:

chrempnochrfnameChrlname
2010AJohn Doe
2010BJean Doe


Now that we are done creating a table, we can now link to it by using OleDbDataAdapter in Visual F#:


1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing and System.Data from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  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  
  5. open System.Windows.Forms  
  6. open System.Data  
  7. open System.Drawing  
  8. //creates a font  
  9. let ffont=new Font("Verdana", 9.75F,FontStyle.Italic, GraphicsUnit.Point)    
  10. //creates a connection object  
  11. let oleconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;  
  12. Data Source=C:\Documents and Settings\station 2\My Documents\dbEmployee.mdb")  
  13. //creates an OleDbDataAdapter  
  14. let dataadpter = new System.Data.OleDb.OleDbDataAdapter("Select * from tblEmployee", oleconn)  
  15. //generates a dataset  
  16. let dataset11 = new DataSet()  
  17. //fills the dataset with recod values  
  18. dataadpter.Fill(dataset11,"tblEmployee")|>ignore  
  19. //creates a form  
  20. let dataform = new Form(Text="Use OleDbDataAdapter",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen)  
  21. //creates an exit button  
  22. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(190, 170))    
  23. //creates a gridview control  
  24. let datagrid = new DataGridView(Dock=DockStyle.None)  
  25. //opens the connection to the datasource  
  26. oleconn.Open()  
  27. //assign our table  
  28. //as a datagrid datasource  
  29. datagrid.DataSource <- dataset11.Tables.["tblEmployee"]  
  30. //add the controls to our form  
  31. dataform.Font<-ffont  
  32. dataform.Controls.Add(datagrid)  
  33. dataform.Controls.Add(exitbutton)  
  34. //when the exit button is clicked  
  35. exitbutton.Click.Add(fun exit->  
  36. //close the form and dataconnection  
  37. dataform.Close()  
  38. oleconn.Close())  
  39.   
  40. //executes our application  
  41. dataform.Show()  
  42. Application.Run(dataform)  


5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:


Wrapping the contents submitted from a textarea in PHP

By default, the texts submitted from a textarea in PHP are displayed in an unbroken single line. For instance, if you have entered the following lorem ipsum text:



It will be displayed as:



This simply tells us that PHP ignores the breaklines posted from a textarea control. This glitch cannot be solved by simply using the WRAP attribute of the textarea tag. Fortunately, PHP provide us with several string functions that can use to solve this problem. One of these functions is the wordwrap function which wraps text depending on the number of character specified by the user. The wordwrap function has the following syntax:

  1. wordwrap(“text to wrap”, columnwidth ,breakline);  
For instance:

  1. wordwrap(“Hello world”,5,"  
  2. ")  
For a sample usage of wordwrap in solving the textarea wrapping problem, follow these steps:

1. Start your text editor (Notepad, Notepad++, or Programmers Notepad).
2. Enter the following:

  1. <HTML>  
  2. <body>  
  3.   
  4. <!--if the submit button is not yet clicked-->  
  5. <?php  
  6. if(!$_POST['Submitted'])  
  7. :  
  8. ?>  
  9.   
  10. <!--display our form-->  
  11. <FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" Method="POST" >  
  12.   
  13.   
  14.   Enter your message:  
  15.   
  16. <TEXTAREA  WRAp="hard" ROWS="8" COLS="30" NAME="usermsg"></TEXTAREA>  
  17.   
  18. <INPUT TYPE="Submit" VALUE="Submit" Name="Submitted"></INPUT> <INPUT TYPE="Reset" VALUE="Clear">  
  19. </FORM>  
  20.   
  21.   
  22. </BODY>  
  23. </HTML>  
  24.   
  25.   
  26. <!--otherwise-->  
  27. <?php  
  28. else  
  29. :  
  30. ?>  
  31.   
  32.   
  33. <!--display the text from the textarea-->  
  34. <?php  
  35. $msg=$_POST['usermsg'];  
  36. $wrapmsg=wordwrap($msg,30,"  
  37. ");  
  38. echo "$msg";  
  39. endif;  
  40. ?>  
  41. ?>  

3. Save it as sample.php in the htdocs folder.
4. Launch your web browser.
5. Type the following in the address bar:
  1. http://localhost/sample.php  
6. You should now see the following:



7. Try entering a long text in the textarea to see the wordwrap effect. For more information on using the wordwrap string function visit http://www.php.net. That's all!

Changing the Pointer when the mouse hovers an object(Visual F#)

To change the mouse pointer when the mouse hovers an object, use the Cursor property. All form controls in Visual F# has Cursor property. To use the Cursor property, use the following syntax:
  1. Objectvariable.Cursor<-Cursors.CursorStyle  
For example:
  1. exitbutton.Cursor<-Cursors.UpArrow  
Some CursorStyles values are NoMove2D,Cross and PanSouth. To understand the usage of the Cursor property, try the following example: 1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008. 2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category. 3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab. 4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. // Learn more about F# at http://fsharp.net  
  2. //specify the namespace memory location  
  3. //of the classes that will be needed in our application  
  4. open System  
  5. open System.Drawing  
  6. open System.Windows.Forms  
  7. //create our controls  
  8. let ffont=new System.Drawing.Font("Microsoft Sans Serif",9.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)  
  9. let myform=new Form(Text="Use Cursors",AutoScaleDimensions=new System.Drawing.SizeF(60.0F, 13.0F),ClientSize=new System.Drawing.Size(207, 133),StartPosition=FormStartPosition.CenterScreen)  
  10. let llabel1=new Label(Text="Hand Cursor",AutoSize=true,Location=new System.Drawing.Point(20, 80))  
  11. let llabel2=new Label(Text="AppStarting Cursor",AutoSize=true,Location=new System.Drawing.Point(120,80))  
  12. //add the controls into our form  
  13. myform.Controls.Add(llabel1)  
  14. myform.Controls.Add(llabel2)  
  15. //add a mousehover and mouseleave events to our controls  
  16. //when the mouse hovers our labels, its cursor style,forecolor, and font size will change  
  17. //when the mouse leaves our control, the cursor style, label forecolor, and font size will  
  18. //return back to default  
  19. llabel1.MouseHover.Add(fun disphandcursor->  
  20.                   llabel1.ForeColor<-Color.Red  
  21.                   llabel1.Cursor<-Cursors.Hand  
  22.                   let ffont=new System.Drawing.Font("Microsoft Sans Serif",12.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)  
  23.                   llabel1.Font<-ffont)  
  24. llabel1.MouseLeave.Add(fun changefontsize->  
  25.                   llabel1.ForeColor<-Color.Empty  
  26.                   let ffont=new System.Drawing.Font("Microsoft Sans Serif",9.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)  
  27.                   llabel1.Font<-ffont)  
  28. llabel2.MouseHover.Add(fun dispappcursor->  
  29.                   llabel2.ForeColor<-Color.Red  
  30.                   llabel2.Cursor<-Cursors.AppStarting  
  31.                   let ffont=new System.Drawing.Font("Microsoft Sans Serif",12.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)  
  32.                   llabel2.Font<-ffont)  
  33. llabel2.MouseLeave.Add(fun retainsfontsize->  
  34.                   llabel2.ForeColor<-Color.Empty  
  35.                   let ffont=new System.Drawing.Font("Microsoft Sans Serif",9.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)  
  36.                   llabel2.Font<-ffont)                
  37. myform.Show()  
  38. //executes our application  
  39. Application.Run(myform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

TabControl(Visual F#)

TabControl is a control that can be used to organize information. It can be utilized as a good alternative to menustrip control. To Create a TabControl in Visual F#, use the folowing syntax:
  1. let tabcontrolobjvariable=new TabControl()  
For example:
  1. let tabcontrol=new TabControl()  
To add a page to your tab control, use the Create an object from the TabPage class using the following syntax:
  1. Let tabpageobjvariable=new TabPage()  
For example:
  1. Let tabpage=new TabPage()  
For a simple example on using TabControl, follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.
2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.
3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab.
4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use the drawing classes  
  5. open System.Drawing  
  6. //specifies the location of the form class  
  7. open System.Windows.Forms  
  8. //specifies the font face and style  
  9. let ffont=new Font("Arial", 9.75F,FontStyle.Italic, GraphicsUnit.Point)  
  10. //creates a form  
  11. let tabform=new Form(Text="Use TabControl",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(250,250),FormBorderStyle=FormBorderStyle.FixedSingle)  
  12. //creates a tab control set its TabSizeMode to fixed  
  13. let tabcontrol=new TabControl(SizeMode=TabSizeMode.Fixed,Location=new System.Drawing.Point(20, 20),Size=new System.Drawing.Size(214, 192))  
  14. //creates a new tab page  
  15. let page1=new TabPage(Text="First Tab",Cursor=Cursors.Hand)  
  16. //creates a new label  
  17. let page1label=new Label(Text="This is page1",Font=ffont,Location=new System.Drawing.Point(20, 20))  
  18. //creates another tab page  
  19. let page2=new TabPage(Text="Second Tab",Cursor=Cursors.Hand)  
  20. //creates another label  
  21. let page2label=new Label(Text="This is page2",Font=ffont,Location=new System.Drawing.Point(20, 20))  
  22. //adds the tabcontrol to our form  
  23. tabform.Controls.Add(tabcontrol)  
  24. //adds the tab pages to our tab control  
  25. tabcontrol.Controls.Add(page1)  
  26. //adds the label to the first page  
  27. page1.Controls.Add(page1label)  
  28. //adds the second page to our tab control  
  29. tabcontrol.Controls.Add(page2)  
  30. //adds the second label to our tab control  
  31. page2.Controls.Add(page2label)  
  32. tabform.Show()  
  33. //executes our application  
  34. Application.Run(tabform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

Property Grid Control(Visual F#)

A property grid cotrol is a control used to display the properties associated to an object. To make a property grid control, use the following syntax:
  1. Let propertygridobjvariable=new PropertyGrid()  
For example:
  1. let props=new PropertyGrid()  
One of the important property is the SelectedObject property which defines the name of the object from which the grid control will get its returned properties. The syntax for SelectedObject property is:
  1. Propertygridvariable.SelectedObject<-Objvariable  
For example:
  1. Props.SelectedObject<-button  
For the sake of example, follow these steps: 1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008. 2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category. 3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab. 4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. // Learn more about F# at http://fsharp.net  
  2. //use the F# library  
  3. open System  
  4. //use the drawing class. Enables intellisense  
  5. open System.Drawing  
  6. //use the form class  
  7. open System.Windows.Forms  
  8. //creates a new form  
  9. let myform=new Form(Text="Use Process Grid",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a textbox  
  11. let txtbox=new TextBox(Text="Textbox", Location=new System.Drawing.Point(5, 240))  
  12. //creates a label  
  13. let lbl=new Label(Text="Label", Location=new System.Drawing.Point(120, 240),AutoSize=true,BorderStyle=BorderStyle.FixedSingle)  
  14. //make a button from the button class  
  15. let button=new Button(Text="Button", Location=new System.Drawing.Point(200, 240))  
  16. //make a property grid control  
  17. let props=new PropertyGrid(Size=new System.Drawing.Size(180, 220),Left=50)  
  18. //adds a descriptive label  
  19. let desc=new Label(Text="Click the control below to view its properties...",AutoSize=true,Top=220)  
  20. //add the controls to our form  
  21. myform.Controls.Add(txtbox)  
  22. myform.Controls.Add(lbl)  
  23. myform.Controls.Add(button)  
  24. myform.Controls.Add(props)  
  25. myform.Controls.Add(desc)  
  26. //when the txtbox is clicked,display its properties in the property grid  
  27. txtbox.Click.Add(fun txtprops->props.SelectedObject<-txtbox)  
  28. //when the label is clicked,display its properties in the property grid  
  29. lbl.Click.Add(fun txtprops->props.SelectedObject<-lbl)  
  30. //when the button is clicked,display its properties in the property grid  
  31. button.Click.Add(fun txtprops->props.SelectedObject<-button)  
  32. myform.Show()  
  33. //executes our application  
  34. Application.Run(myform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

Picturebox Control(Visual F#)

Picturebox is a control used to display an image on your form. To create a picturebox, use the following syntax:
  1. let pictureboxobjvariable=new PictureBox()  
for instance:
  1. let picbox=new PictureBox()  
To specify the url of an image to display in the picturebox, use the Image property.

Syntax:
  1. pictureboxobjvariable.ImageLocation<-"url of an existent image"  
for instance:
  1. picbox.ImageLocation<-"C:\myimage.png"  
Just make sure that myimage.jpg exist on your computer. Follow the steps below for a simple example on using a picturebox. 1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008. 2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category. 3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms. Do step 3 again and this time, select System.Drawing from the .Net tab. 4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. //use the f# standard library  
  2. // Learn more about F# at http://fsharp.net  
  3.   
  4. open System  
  5. //use this to enable the intellisense. Very helpful in coding your application  
  6. open System.Drawing   
  7. //specify the location of the Form classes  
  8. open System.Windows.Forms   
  9. //create a form named imageform  
  10. let imageform=new Form(Text="Use PictureBox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates a horizontal scrollbar  
  12. let hscroll=new HScrollBar(Location=new System.Drawing.Point(10, 230),Width=270)  
  13. //creates a tooltip  
  14. let tip=new ToolTip(IsBalloon=true,ToolTipIcon=ToolTipIcon.Info)  
  15. //creates a picturebox named picbox  
  16. //set the sizemode to autosize to adjust the size of the picturebox  
  17. //depending on the size of your image  
  18. //sets the picturebox borderstyle to fixed single  
  19. let picbox=new PictureBox(SizeMode=PictureBoxSizeMode.AutoSize,BorderStyle=BorderStyle.FixedSingle,Top=5)  
  20. picbox.ImageLocation<-("C:\suzzane.png")  
  21. //adds the picturebox on your form  
  22. imageform.Controls.Add(picbox)  
  23. imageform.Controls.Add(hscroll)  
  24. //adds the scrollbar to our form  
  25. //displays a tooltip when the mouse hovers the scrollbar  
  26. hscroll.MouseHover.Add(fun desc->tip.SetToolTip(hscroll,"Drag the scroll box to move the image"))  
  27. //change the horizontal position of the image when the scrollbox is moved  
  28. hscroll.Scroll.Add(fun move->picbox.Left<-Convert.ToInt32(hscroll.Value.ToString()))  
  29. imageform.Show()  
  30. Application.Run(imageform)  
  31. //executes your application  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx

Using a timer control in Visual F#

Timer controls are used to automatically execute an action after a specified time has elapsed. To create a timer control in F#, use the following syntax:
  1. let timerobjvariable=new Timer()  
For instance:
  1. let timer=new Timer()  
For a simple example on using a timer control, follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  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. //creates a form  
  9. let timerform=new Form(Text="Use Timer",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //use the random function to generate random numbers  
  11. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220))  
  12. let random=new Random()  
  13. //create a timer object and set its interval to 1 second  
  14. //by default timer are disabled so you'll need to enable it  
  15. let timer1=new Timer(Interval=1000,Enabled=true)  
  16. //assigns a random backcolor to our form   
  17. //change it every 1 second  
  18. timer1.Tick.Add(fun time->timerform.BackColor<-Color.FromArgb(random.Next(0,255),random.Next(0,255),random.Next(0,255)))  
  19. //adds the exit button to our form  
  20. timerform.Controls.Add(exitbutton)  
  21. //when the exit button is clicked  
  22. exitbutton.Click.Add(fun quit->  
  23. //stops the time  
  24. timer1.Stop()  
  25. //close the form  
  26. timerform.Close())                     
  27. //show our form  
  28. timerform.Show()  
  29. //execute our application  
  30. Application.Run(timerform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:



If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx

Activating a new Form at run-time (Visual F#)

To activate a new form at run-time, simply use the Show and Hide methods of the form. The following steps demonstrate a simple example:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do step 3 again and this time, select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. //use the F# standard library  
  2. open System  
  3. //use the drawing class  
  4. open System.Drawing   
  5. //Specifies the memory location of the form class  
  6. open System.Windows.Forms   
  7. //create a font object  
  8. let ffont=new Font("Arial",12.0f,FontStyle.Bold, GraphicsUnit.Point)  
  9. //creates a form named “firstform”  
  10. let firstform=new Form(Text="Display First Form",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)  
  11. //creates a label,sets its caption to “This is the first form”  
  12. let fformlabel=new Label(Text="This is the first form",AutoSize=true)  
  13. //make a button,sets its caption to Second Form  
  14. let nxtbutton=new Button(Text="Second Form", Location=new System.Drawing.Point(100, 200),AutoSize=true)  
  15. //creates another button, set its text to exit  
  16. let fexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200),AutoSize=true)  
  17. //adds the controls we made to the first form  
  18. firstform.Controls.Add(fformlabel)  
  19. //applies font format to our label  
  20. fformlabel.Font<-ffont  
  21. firstform.Controls.Add(nxtbutton)  
  22. firstform.Controls.Add(fexitbutton)  
  23. //when the “Second Form” button of the first form is clicked  
  24. nxtbutton.Click.Add(fun nxtform->  
  25. //hides the first form  
  26. firstform.Hide()  
  27. //create a new form named secondform  
  28. let secondform=new Form(Text="Second Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)  
  29. //create a label and sets its caption to “This is the second form”  
  30. let sformlabel=new Label(Text="This is the second form",AutoSize=true)  
  31. //creates a button and sets its text to “First Form”  
  32. let prevbutton=new Button(Text="First Form", Location=new System.Drawing.Point(100, 200))  
  33. //adds a button, set its text to “Exit”  
  34. let sexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  35. //show the second form  
  36. secondform.Show()  
  37. //add the controls we made on the second form  
  38. secondform.Controls.Add(sformlabel)  
  39. //applies font format to our label  
  40. sformlabel.Font<-ffont  
  41. secondform.Controls.Add(prevbutton)  
  42. secondform.Controls.Add(sexitbutton)  
  43. //when the “First Form” button of the second form is clicked  
  44. prevbutton.Click.Add(fun prevform->  
  45. //show the first form  
  46. firstform.Show()  
  47. //hide the second form  
  48. secondform.Hide())  
  49. //when the exit button of the second form is clicked  
  50. sexitbutton.Click.Add(fun quit->  
  51. //close the first form  
  52. firstform.Close()  
  53. //close the second form  
  54. secondform.Close()))  
  55. //when the exit button of the first form is clicked  
  56. //close the first form  
  57. fexitbutton.Click.Add(fun quit->firstform.Close())                       
  58. firstform.Show()  
  59. //executes our application  
  60. Application.Run(firstform)  
Here is our sample code again and this time comments omitted.
  1. open System  
  2. open System.Drawing    
  3. open System.Windows.Forms  
  4. let ffont=new Font("Arial",12.0f,FontStyle.Bold, , GraphicsUnit.Point)   
  5. let firstform=new Form(Text="First Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)  
  6. let fformlabel=new Label(Text="This is the first form",AutoSize=true)  
  7. let nxtbutton=new Button(Text="Second Form", Location=new System.Drawing.Point(100, 200),AutoSize=true)  
  8. let fexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200),AutoSize=true)  
  9. firstform.Controls.Add(fformlabel)  
  10. fformlabel.Font<-ffont  
  11. firstform.Controls.Add(nxtbutton)  
  12. firstform.Controls.Add(fexitbutton)  
  13.   
  14. nxtbutton.Click.Add(fun nxtform->  
  15. firstform.Hide()  
  16. let secondform=new Form(Text="Second Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)  
  17. let sformlabel=new Label(Text="This is the second form",AutoSize=true)  
  18. let prevbutton=new Button(Text="First Form", Location=new System.Drawing.Point(100, 200))  
  19. let sexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))  
  20. secondform.Show()  
  21. secondform.Controls.Add(sformlabel)  
  22. sformlabel.Font<-ffont  
  23. secondform.Controls.Add(prevbutton)  
  24. secondform.Controls.Add(sexitbutton)  
  25. prevbutton.Click.Add(fun prevform->  
  26. firstform.Show()  
  27. secondform.Hide())  
  28. sexitbutton.Click.Add(fun quit->  
  29. firstform.Close()  
  30. secondform.Close()))    
  31.   
  32. fexitbutton.Click.Add(fun quit->firstform.Close())                       
  33. firstform.Show()  
  34. Application.Run(firstform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

MonthCalendar Control(Visual F#)

MonthCalendar control is essentially used to display monthly calendar on your application. To make a month calendar control, follow the following syntax:
  1. Let calendarobjvariable=new MonthCalendar()  
Example:
  1. Let calendar=new MonthCalendar()  
For a simple example on using month calendar follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.Do step 3 again and this time, select System.Drawing.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. //use the F# standard library  
  2. open System  
  3. open System.Drawing  
  4. open System.Windows.Forms   
  5. //sets the font face and font style of our calendar  
  6. let myfont=new Font("Arial",11.0f,FontStyle.Regular, GraphicsUnit.Point)  
  7. //create a form  
  8. let calendarform=new Form(Text="Use MonthCalendar",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  9. //create a calendar object named calendar  
  10. //sets its minimum date to jan 1 1999   
  11. //set the maximum date displayed to it to Jan 1,8013  
  12. let calendar=new MonthCalendar(MinDate=Convert.ToDateTime("1 Jan,1999"),MaxDate=Convert.ToDateTime("1 Jan,8013"),Location=new System.Drawing.Point(30,10))  
  13. //change the title backcolor to crimson  
  14. calendar.TitleBackColor<-Color.Crimson  
  15. //applies the font to our calendar  
  16. calendar.Font<-myfont  
  17. //change the calendar backcolor to Bisque  
  18. calendar.BackColor<-Color.Bisque  
  19. //adds the calendar control to our form  
  20. calendarform.Controls.Add(calendar)  
  21. calendarform.Show()  
  22. //executes our application  
  23. Application.Run(calendarform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot: If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx

Adding ToolTips(Visual F# Windows Forms Application)

Tooltips are typically used to display a descriptive text when the mouse pointer hovers a control. To create a tooltip, use the following syntax:
  1. let tooltipobjvaribale=new ToolTip()  
For example:
  1. let tip=new ToolTip()  
One of the essential methods of the ToolTip object is the SetToolTip method which allows user to connect the tooltip to a specified control. SetToolTip has the following syntax:
  1. tooltipobjvariable.SetToolTip(object variable of the control where you wanted to display the tooltip,”tooltiptext”)  
Example:
  1. tip.SetToolTip(addbutton,"Click here to add”)  

The following steps demonstrates a simple example in using ToolTips.

The following example demonstrate a simple application using WebBrowser control:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. //uses the F# standard library  
  2. open System  
  3. //use the drawing classes   
  4. open System.Drawing  
  5. //specifies the location of the form class  
  6. open System.Windows.Forms   
  7. //make a form add set its caption to “Add Tooltip”  
  8. let myfont=new Font("Arial",8.0f,FontStyle.Bold, GraphicsUnit.Point)  
  9. let tipform=new Form(Text="Add Tooltip",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //create a label  
  11. let desclabel=new Label(Text="Hover your mouse on the button...",Width=400,AutoSize=false)  
  12. //make a button and assign an “Exit” to it  
  13. let exitbutton=new Button(Text="Exit",Location=new System.Drawing.Point(200,220))  
  14. //create a tooltip and set its Title to “Use ToolTip”  
  15. //enable the balloon form  
  16. //add a warning icon. Other icon values are warning and info  
  17. let tip=new ToolTip(ToolTipTitle="Use ToolTip",IsBalloon=true,ToolTipIcon=ToolTipIcon.Warning)  
  18. //add the controls to our form  
  19. tipform.Controls.Add(desclabel)  
  20. desclabel.Font<-myfont  
  21. desclabel.ForeColor<-Color.Red  
  22. tipform.Controls.Add(exitbutton)  
  23. //when the mouse hovers the exit button  
  24. exitbutton.MouseHover.Add(fun tipmsg->  
  25. //display our tip  
  26. tip.SetToolTip(exitbutton,"Click here to quit"))  
  27. //quits the form when the exit button is clicked  
  28. exitbutton.Click.Add(fun quit->tipform.Close())  
  29. tipform.Show()  
  30. //executes our application  
  31. Application.Run(tipform)  
5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

WebBrowser control(Visual F# Windows Forms Application)

WebBrowser control is a control used to display a web page or web document. To create a WebBrowser control in F#, use the following syntax:

  1. let webbrowserobjvaribale=new WebBrowser()  
For instance:
  1. let browser=new WebBrowser()  
One of the important methods of the WebBrowser object is the Navigate method which is used to navigate and display the specified URL. Navigate method has the following syntax:
  1. Webbrowserobjvaribale.Navigate(“url”)  
For Example:
  1. Webbrowserobjvaribale.Navigate(“www.google.com”)  
The following example demonstrate a simple application using WebBrowser control:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do step 3 again and this time select System.Drawing.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. // Learn more about F# at http://fsharp.net  
  2. //uses the standard F# library  
  3. open System  
  4. //use the drawing classes  
  5. open System.Drawing   
  6. //specify the namespace memory location of the form class  
  7. open System.Windows.Forms  
  8. //creates a new font  
  9. let myfont=new Font("Arial",8.0f,FontStyle.Regular,GraphicsUnit.Point)   
  10. //creates a new form  
  11. let webform=new Form(Text="Use WebBrowser",Width=400,StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,FormBorderStyle=FormBorderStyle.FixedSingle)  
  12. let addresslabel=new Label(Location=new System.Drawing.Point(15, 5),Text="Enter URL:",AutoSize=true)  
  13. let urltextbox=new TextBox(Location=new System.Drawing.Point(80, 5))  
  14. let openpgbutton=new Button(Text="Open Page",Location=new System.Drawing.Point(190, 5))  
  15. let opennpbutton=new Button(Text="New Window",Location=new System.Drawing.Point(280, 5),AutoSize=true)  
  16. //create a webbrowser control and change its dock property to none  
  17. //so that it will not occupy the whole form  
  18. let browser=new WebBrowser(Dock=DockStyle.None,Size=new System.Drawing.Size(300, 200),Location=new System.Drawing.Point(40, 30),MinimumSize=new System.Drawing.Size(20, 20))                 
  19. //changes the font to arial  
  20. webform.Font<-myfont  
  21. //adds the controls into our form  
  22. webform.Controls.Add(addresslabel)  
  23. webform.Controls.Add(urltextbox)  
  24. webform.Controls.Add(openpgbutton)  
  25. webform.Controls.Add(opennpbutton)  
  26. webform.Controls.Add(browser)  
  27. //when the form is loaded  
  28. webform.Load.Add(fun defa->  
  29.             //display the google page  
  30.             browser.Navigate("www.google.com",false))   
  31. //locate and navigate the webaddress inpputed in the textbox  
  32. openpgbutton.Click.Add(fun openpage->  
  33.             browser.Navigate(urltextbox.Text,false))  
  34. //opens the page in a new window  
  35. opennpbutton.Click.Add(fun openpageinnewwindow->  
  36.             browser.Navigate(urltextbox.Text,true))  
  37. webform.Show()  
  38. [<STAThread>]  
  39. //executes our application  
  40. Application.Run(webform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:



If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx

ListBox Control( Visual F#’s Windows Form Application)

Listbox are normally used to present a list of data to the user. To create a listbox, use the following syntax:
  1. Let listboxobjvariable=new Listbox()  
For instance:
  1. Let listbox=new Listbox()  
One of the most important methods of the ListBox object is the Add method which allows user to add an item into the listbox. Add method has the following syntax:
  1. listboxobjvariable=new ListBox()  
For instance:
  1. listbox.Items.Add(“Hyberdabad”)  
There are several usable properties and methods of the listbox object apart from the add method which includes SelectedItem(displays the listbox item text),SelectedIndex(returns the index number of the selected item),Count(counts the number of items),RemoveAt(Deletes an item),and Clear(wipes out the contents of the listbox). In the preceding example, we will just be using the Add method. Just follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do Step 3 again and select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. //use the f# standard library  
  2. open System  
  3. //specify the memory location of the classes used in drawing objects  
  4. //required to draw the listbox item text  
  5. open System.Drawing  
  6. //specify the location of the form class  
  7. open System.Windows.Forms  
  8. //creates a form and assign a Use listbox function to it  
  9. let sampleform=new Form(Text="Use Listbox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a label and set its Text to “Count”  
  11. let lbl=new Label(Text="Country:", Location=new System.Drawing.Point(20,10),AutoSize=true)  
  12. //makes a listbox  
  13. let countrylistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)  
  14. //adds an item to the listbox when the form is loaded  
  15. sampleform.Load.Add(fun i->  
  16.      //adds the items and ignore the passed index position values  
  17.                     countrylistbox.Items.Add("United States")|>ignore  
  18.                     countrylistbox.Items.Add("Philippines")|>ignore  
  19.                     countrylistbox.Items.Add("India")|>ignore  
  20.                     countrylistbox.Items.Add("Lithuania")|>ignore  
  21.                     countrylistbox.Items.Add("Germany")|>ignore  
  22.                     countrylistbox.Items.Add("Indonesia")|>ignore)  
  23. //displays the label to our form  
  24. sampleform.Controls.Add(lbl)        
  25. //adds the listbox to our form      
  26. sampleform.Controls.Add(countrylistbox)             
  27. sampleform.Show()  
  28. //executes the application  
  29. Application.Run(sampleform)  

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:




If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx