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