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:

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:
Let calendarobjvariable=new MonthCalendar()
Example:
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 “:

//use the F# standard library
open System
open System.Drawing
open System.Windows.Forms 
//sets the font face and font style of our calendar
let myfont=new Font("Arial",11.0f,FontStyle.Regular, GraphicsUnit.Point)
//create a form
let calendarform=new Form(Text="Use MonthCalendar",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//create a calendar object named calendar
//sets its minimum date to jan 1 1999 
//set the maximum date displayed to it to Jan 1,8013
let calendar=new MonthCalendar(MinDate=Convert.ToDateTime("1 Jan,1999"),MaxDate=Convert.ToDateTime("1 Jan,8013"),Location=new System.Drawing.Point(30,10))
//change the title backcolor to crimson
calendar.TitleBackColor<-Color.Crimson
//applies the font to our calendar
calendar.Font<-myfont
//change the calendar backcolor to Bisque
calendar.BackColor<-Color.Bisque
//adds the calendar control to our form
calendarform.Controls.Add(calendar)
calendarform.Show()
//executes our application
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:
let tooltipobjvaribale=new ToolTip()
For example:
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:
tooltipobjvariable.SetToolTip(object variable of the control where you wanted to display the tooltip,”tooltiptext”)
Example:
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 “:

//uses the F# standard library
open System
//use the drawing classes 
open System.Drawing
//specifies the location of the form class
open System.Windows.Forms 
//make a form add set its caption to “Add Tooltip”
let myfont=new Font("Arial",8.0f,FontStyle.Bold, GraphicsUnit.Point)
let tipform=new Form(Text="Add Tooltip",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//create a label
let desclabel=new Label(Text="Hover your mouse on the button...",Width=400,AutoSize=false)
//make a button and assign an “Exit” to it
let exitbutton=new Button(Text="Exit",Location=new System.Drawing.Point(200,220))
//create a tooltip and set its Title to “Use ToolTip”
//enable the balloon form
//add a warning icon. Other icon values are warning and info
let tip=new ToolTip(ToolTipTitle="Use ToolTip",IsBalloon=true,ToolTipIcon=ToolTipIcon.Warning)
//add the controls to our form
tipform.Controls.Add(desclabel)
desclabel.Font<-myfont
desclabel.ForeColor<-Color.Red
tipform.Controls.Add(exitbutton)
//when the mouse hovers the exit button
exitbutton.MouseHover.Add(fun tipmsg->
//display our tip
tip.SetToolTip(exitbutton,"Click here to quit"))
//quits the form when the exit button is clicked
exitbutton.Click.Add(fun quit->tipform.Close())
tipform.Show()
//executes our application
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:

let webbrowserobjvaribale=new WebBrowser()
For instance:
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:
Webbrowserobjvaribale.Navigate(“url”)
For Example:
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 “:
// Learn more about F# at http://fsharp.net
//uses the standard F# library
open System
//use the drawing classes
open System.Drawing 
//specify the namespace memory location of the form class
open System.Windows.Forms
//creates a new font
let myfont=new Font("Arial",8.0f,FontStyle.Regular,GraphicsUnit.Point) 
//creates a new form
let webform=new Form(Text="Use WebBrowser",Width=400,StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,FormBorderStyle=FormBorderStyle.FixedSingle)
let addresslabel=new Label(Location=new System.Drawing.Point(15, 5),Text="Enter URL:",AutoSize=true)
let urltextbox=new TextBox(Location=new System.Drawing.Point(80, 5))
let openpgbutton=new Button(Text="Open Page",Location=new System.Drawing.Point(190, 5))
let opennpbutton=new Button(Text="New Window",Location=new System.Drawing.Point(280, 5),AutoSize=true)
//create a webbrowser control and change its dock property to none
//so that it will not occupy the whole form
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))               
//changes the font to arial
webform.Font<-myfont
//adds the controls into our form
webform.Controls.Add(addresslabel)
webform.Controls.Add(urltextbox)
webform.Controls.Add(openpgbutton)
webform.Controls.Add(opennpbutton)
webform.Controls.Add(browser)
//when the form is loaded
webform.Load.Add(fun defa->
            //display the google page
            browser.Navigate("www.google.com",false)) 
//locate and navigate the webaddress inpputed in the textbox
openpgbutton.Click.Add(fun openpage->
            browser.Navigate(urltextbox.Text,false))
//opens the page in a new window
opennpbutton.Click.Add(fun openpageinnewwindow->
            browser.Navigate(urltextbox.Text,true))
webform.Show()
[<STAThread>]
//executes our application
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:
Let listboxobjvariable=new Listbox()
For instance:
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:
listboxobjvariable=new ListBox()
For instance:
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 “:
//use the f# standard library
open System
//specify the memory location of the classes used in drawing objects
//required to draw the listbox item text
open System.Drawing
//specify the location of the form class
open System.Windows.Forms
//creates a form and assign a Use listbox function to it
let sampleform=new Form(Text="Use Listbox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label and set its Text to “Count”
let lbl=new Label(Text="Country:", Location=new System.Drawing.Point(20,10),AutoSize=true)
//makes a listbox
let countrylistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)
//adds an item to the listbox when the form is loaded
sampleform.Load.Add(fun i->
     //adds the items and ignore the passed index position values
                    countrylistbox.Items.Add("United States")|>ignore
                    countrylistbox.Items.Add("Philippines")|>ignore
                    countrylistbox.Items.Add("India")|>ignore
                    countrylistbox.Items.Add("Lithuania")|>ignore
                    countrylistbox.Items.Add("Germany")|>ignore
                    countrylistbox.Items.Add("Indonesia")|>ignore)
//displays the label to our form
sampleform.Controls.Add(lbl)      
//adds the listbox to our form    
sampleform.Controls.Add(countrylistbox)           
sampleform.Show()
//executes the application
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