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:
Let propertygridobjvariable=new PropertyGrid()
For example:
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:
Propertygridvariable.SelectedObject<-Objvariable
For example:
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 “:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use the drawing class. Enables intellisense
open System.Drawing
//use the form class
open System.Windows.Forms
//creates a new form
let myform=new Form(Text="Use Process Grid",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a textbox
let txtbox=new TextBox(Text="Textbox", Location=new System.Drawing.Point(5, 240))
//creates a label
let lbl=new Label(Text="Label", Location=new System.Drawing.Point(120, 240),AutoSize=true,BorderStyle=BorderStyle.FixedSingle)
//make a button from the button class
let button=new Button(Text="Button", Location=new System.Drawing.Point(200, 240))
//make a property grid control
let props=new PropertyGrid(Size=new System.Drawing.Size(180, 220),Left=50)
//adds a descriptive label
let desc=new Label(Text="Click the control below to view its properties...",AutoSize=true,Top=220)
//add the controls to our form
myform.Controls.Add(txtbox)
myform.Controls.Add(lbl)
myform.Controls.Add(button)
myform.Controls.Add(props)
myform.Controls.Add(desc)
//when the txtbox is clicked,display its properties in the property grid
txtbox.Click.Add(fun txtprops->props.SelectedObject<-txtbox)
//when the label is clicked,display its properties in the property grid
lbl.Click.Add(fun txtprops->props.SelectedObject<-lbl)
//when the button is clicked,display its properties in the property grid
button.Click.Add(fun txtprops->props.SelectedObject<-button)
myform.Show()
//executes our application
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:
let pictureboxobjvariable=new PictureBox()
for instance:
let picbox=new PictureBox()
To specify the url of an image to display in the picturebox, use the Image property.

Syntax:
pictureboxobjvariable.ImageLocation<-"url of an existent image"
for instance:
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 “:
//use the f# standard library
// Learn more about F# at http://fsharp.net

open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms 
//create a form named imageform
let imageform=new Form(Text="Use PictureBox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a horizontal scrollbar
let hscroll=new HScrollBar(Location=new System.Drawing.Point(10, 230),Width=270)
//creates a tooltip
let tip=new ToolTip(IsBalloon=true,ToolTipIcon=ToolTipIcon.Info)
//creates a picturebox named picbox
//set the sizemode to autosize to adjust the size of the picturebox
//depending on the size of your image
//sets the picturebox borderstyle to fixed single
let picbox=new PictureBox(SizeMode=PictureBoxSizeMode.AutoSize,BorderStyle=BorderStyle.FixedSingle,Top=5)
picbox.ImageLocation<-("C:\suzzane.png")
//adds the picturebox on your form
imageform.Controls.Add(picbox)
imageform.Controls.Add(hscroll)
//adds the scrollbar to our form
//displays a tooltip when the mouse hovers the scrollbar
hscroll.MouseHover.Add(fun desc->tip.SetToolTip(hscroll,"Drag the scroll box to move the image"))
//change the horizontal position of the image when the scrollbox is moved
hscroll.Scroll.Add(fun move->picbox.Left<-Convert.ToInt32(hscroll.Value.ToString()))
imageform.Show()
Application.Run(imageform)
//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:
let timerobjvariable=new Timer()
For instance:
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 “:
// Learn more about F# at http://fsharp.net
//use the F# library
open System
//use this to enable the intellisense. Very helpful in coding your application
open System.Drawing 
//specify the location of the Form classes
open System.Windows.Forms
//creates a form
let timerform=new Form(Text="Use Timer",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//use the random function to generate random numbers
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220))
let random=new Random()
//create a timer object and set its interval to 1 second
//by default timer are disabled so you'll need to enable it
let timer1=new Timer(Interval=1000,Enabled=true)
//assigns a random backcolor to our form 
//change it every 1 second
timer1.Tick.Add(fun time->timerform.BackColor<-Color.FromArgb(random.Next(0,255),random.Next(0,255),random.Next(0,255)))
//adds the exit button to our form
timerform.Controls.Add(exitbutton)
//when the exit button is clicked
exitbutton.Click.Add(fun quit->
//stops the time
timer1.Stop()
//close the form
timerform.Close())                   
//show our form
timerform.Show()
//execute our application
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 “:
//use the F# standard library
open System
//use the drawing class
open System.Drawing 
//Specifies the memory location of the form class
open System.Windows.Forms 
//create a font object
let ffont=new Font("Arial",12.0f,FontStyle.Bold, GraphicsUnit.Point)
//creates a form named “firstform”
let firstform=new Form(Text="Display First Form",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)
//creates a label,sets its caption to “This is the first form”
let fformlabel=new Label(Text="This is the first form",AutoSize=true)
//make a button,sets its caption to Second Form
let nxtbutton=new Button(Text="Second Form", Location=new System.Drawing.Point(100, 200),AutoSize=true)
//creates another button, set its text to exit
let fexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200),AutoSize=true)
//adds the controls we made to the first form
firstform.Controls.Add(fformlabel)
//applies font format to our label
fformlabel.Font<-ffont
firstform.Controls.Add(nxtbutton)
firstform.Controls.Add(fexitbutton)
//when the “Second Form” button of the first form is clicked
nxtbutton.Click.Add(fun nxtform->
//hides the first form
firstform.Hide()
//create a new form named secondform
let secondform=new Form(Text="Second Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)
//create a label and sets its caption to “This is the second form”
let sformlabel=new Label(Text="This is the second form",AutoSize=true)
//creates a button and sets its text to “First Form”
let prevbutton=new Button(Text="First Form", Location=new System.Drawing.Point(100, 200))
//adds a button, set its text to “Exit”
let sexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))
//show the second form
secondform.Show()
//add the controls we made on the second form
secondform.Controls.Add(sformlabel)
//applies font format to our label
sformlabel.Font<-ffont
secondform.Controls.Add(prevbutton)
secondform.Controls.Add(sexitbutton)
//when the “First Form” button of the second form is clicked
prevbutton.Click.Add(fun prevform->
//show the first form
firstform.Show()
//hide the second form
secondform.Hide())
//when the exit button of the second form is clicked
sexitbutton.Click.Add(fun quit->
//close the first form
firstform.Close()
//close the second form
secondform.Close()))
//when the exit button of the first form is clicked
//close the first form
fexitbutton.Click.Add(fun quit->firstform.Close())                     
firstform.Show()
//executes our application
Application.Run(firstform)


Here is our sample code again and this time comments omitted.
open System
open System.Drawing  
open System.Windows.Forms
let ffont=new Font("Arial",12.0f,FontStyle.Bold, , GraphicsUnit.Point) 
let firstform=new Form(Text="First Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)
let fformlabel=new Label(Text="This is the first form",AutoSize=true)
let nxtbutton=new Button(Text="Second Form", Location=new System.Drawing.Point(100, 200),AutoSize=true)
let fexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200),AutoSize=true)
firstform.Controls.Add(fformlabel)
fformlabel.Font<-ffont
firstform.Controls.Add(nxtbutton)
firstform.Controls.Add(fexitbutton)

nxtbutton.Click.Add(fun nxtform->
firstform.Hide()
let secondform=new Form(Text="Second Form", StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(200,200),FormBorderStyle=FormBorderStyle.FixedSingle)
let sformlabel=new Label(Text="This is the second form",AutoSize=true)
let prevbutton=new Button(Text="First Form", Location=new System.Drawing.Point(100, 200))
let sexitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 200))
secondform.Show()
secondform.Controls.Add(sformlabel)
sformlabel.Font<-ffont
secondform.Controls.Add(prevbutton)
secondform.Controls.Add(sexitbutton)
prevbutton.Click.Add(fun prevform->
firstform.Show()
secondform.Hide())
sexitbutton.Click.Add(fun quit->
firstform.Close()
secondform.Close()))  

fexitbutton.Click.Add(fun quit->firstform.Close())                     
firstform.Show()
Application.Run(firstform)


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