Check box, Radio button, and Group box in Visual F#'s Windows Form Application

One of the most commonly used form controls in real-life application is the check box. A check box is a control that allows user to select various options from a group of options. To create a check box in Visual F#, use the following syntax:

  1. let checkboxobjvariable=new CheckBox()  

For instance:
  1. let chk=new CheckBox()  
Some of the essential properties of check box are Text and Checked. The Text property enables user to add a text beside a check box while the Checked property checks or unchecks a check box and can handle the boolean value true or false. Visual F#’s check box shares the same properties with the check boxes in other visual programming languages such as Visual C++, Visual J# and Visual Basic, so if you want to learn its other properties, check the check box properties of other visual programming languages.

Radio button of the other hand, is the complete opposite of check box for the fact that it only allow a single selection among a list of options. To change the text beside the radio button, use the Text property. It also has a Checked property which enables user to tick or untick the radio button.

To create a radio button, use the following syntax:
  1. let radiobuttonobjvariable=new RadioButton()  
For example:
  1. let rdo=new RadioButton()  
To group check boxes and radio buttons, a control called group box is used. Though there are several properties of radio buttons, the most frequently used is the Text property and is used to change the group box header text. To create a group box, use the following syntax:
  1. Let groupboxobjvariable=new GroupBox()  
For instance:
  1. Let grp=new GroupBox()  
Let’s now use the check box, radio button, and group box in a single 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

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 Visual F# library  
  3. open System  
  4. //use System.Drawing  
  5. open System.Drawing  
  6. //specifies the location of the Form classes  
  7. open System.Windows.Forms  
  8. //creates a form named cnbform and assign a “Use Radio Button” caption to it  
  9. let cnbform=new Form(Text="Use Radio Button",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a group box and sets its header text to “RadioButtons”  
  11. //Use the Top and Left properties to align it in the form  
  12. let gbox1=new GroupBox(Text="RadioButtons", Location=new System.Drawing.Point(10,5),Height=80)  
  13. //creates a radio button and change its text to “First radio button”  
  14. //Use the top and left property to align it in the group box  
  15. let radio1=new RadioButton(Text="First radio button", Location=new System.Drawing.Point(20,20),AutoSize=true)  
  16. //creates radio button and set its text to “Second radio button”  
  17. //Use the autosize property to automatically expand the width of text handler  
  18. let radio2=new RadioButton(Text="Second radio button", Location=new System.Drawing.Point(20,40),AutoSize=true)  
  19. //creates a group box and set its text to “CheckBoxes”  
  20. //position the group box in the form by using the Top and Left properties  
  21. let gbox2=new GroupBox(Text="CheckBoxes",Height=80, Location=new System.Drawing.Point(10,100))  
  22. //creates a check box and change its text to “First checkbox”  
  23. //Use the top and left property to align it in the group box  
  24. let check1=new CheckBox(Text="First checkbox", Location=new System.Drawing.Point(20,20),AutoSize=true)  
  25. //creates another check box and change its text to “Second checkbox”  
  26. //Use the top and left property to align it in the group box  
  27.   
  28. let check2=new CheckBox(Text="Second checkbox", Location=new System.Drawing.Point(20,40),AutoSize=true)  
  29. //creates a button and set its text to “Exit”  
  30. let exitbutton=new Button(Location=new System.Drawing.Point(200,200),Text="Exit")  
  31.   
  32. //displays the first group box into the form  
  33. cnbform.Controls.Add(gbox1)  
  34. //adds the radio buttons into the groupbox  
  35. gbox1.Controls.Add(radio1)  
  36. gbox1.Controls.Add(radio2)  
  37.   
  38. //adds the second group box into the form  
  39. cnbform.Controls.Add(gbox2)  
  40. //displays the checkboxes into the second groupbox  
  41. gbox2.Controls.Add(check1)  
  42. gbox2.Controls.Add(check2)  
  43.   
  44. //inserts the exit button into the form  
  45. cnbform.Controls.Add(exitbutton)  
  46. //attach a click event to the first radio button then assign a function to it  
  47. radio1.Click.Add(fun r1msg->  
  48. //check the first checkbox  
  49. check1.Checked<-true  
  50.                //unchecks the first checkbox  
  51.                 check2.Checked<-false)  
  52.   
  53. //attach a click event to the first radio button then assign a function to it  
  54.   
  55. radio2.Click.Add(fun r2msg->  
  56. //check the second checkbox  
  57. check2.Checked<-true  
  58.               //unchecks the first checkbox  
  59.               check1.Checked<-false)  
  60. //quits the form when the exit button is clicked                   
  61. exitbutton.Click.Add(fun bact->cnbform.Close())      
  62. //shows the form         
  63. cnbform.Show()  
  64. //runs the application  
  65. Application.Run(cnbform)  
5. Lets view of our again once again and this time, comments omitted:

  1. // Learn more about F# at http://fsharp.net  
  2. open System  
  3. open System.Drawing  
  4. open System.Windows.Forms  
  5. let cnbform=new Form(Text="Use Radio Button",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  6.   
  7. let gbox1=new GroupBox(Text="RadioButtons", Location=new System.Drawing.Point(10,5),Height=80)  
  8. let radio1=new RadioButton(Text="First radio button", Location=new System.Drawing.Point(20,20),AutoSize=true)  
  9. let radio2=new RadioButton(Text="Second radio button", Location=new System.Drawing.Point(20,40),AutoSize=true)  
  10.   
  11. let gbox2=new GroupBox(Text="CheckBoxes",Height=80, Location=new System.Drawing.Point(10,100))  
  12. let check1=new CheckBox(Text="First checkbox", Location=new System.Drawing.Point(20,20),AutoSize=true)  
  13. let check2=new CheckBox(Text="Second checkbox", Location=new System.Drawing.Point(20,40),AutoSize=true)  
  14.   
  15. let exitbutton=new Button(Location=new System.Drawing.Point(200,200),Text="Exit")  
  16.   
  17.   
  18. cnbform.Controls.Add(gbox1)  
  19. gbox1.Controls.Add(radio1)  
  20. gbox1.Controls.Add(radio2)  
  21.   
  22. cnbform.Controls.Add(gbox2)  
  23. gbox2.Controls.Add(check1)  
  24. gbox2.Controls.Add(check2)  
  25.   
  26. cnbform.Controls.Add(exitbutton)  
  27.   
  28. radio1.Click.Add(fun r1msg->  
  29. check1.Checked<-true  
  30.                     check2.Checked<-false)  
  31. radio2.Click.Add(fun r2msg->  
  32. check2.Checked<-true  
  33.                     check1.Checked<-false)  
  34.                       
  35. exitbutton.Click.Add(fun bact->cnbform.Close())             
  36. cnbform.Show()  
  37. Application.Run(cnbform)  
6. Click the run icon to execute your application. You should now see an output similar to the following screen shot:

Add Two Numbers (Visual F# Windows Forms Application)

I’d been desperately combing the net for a while now looking for some tutorials relating to simple computations in Visual F# Windows Forms Application unfortunately I got no fluke finding some so I decided to make one. Today we will be making a simple application that adds two numbers and I hope that this will give you a head start in building advanced application using F# Windows Forms.
To create an application that adds two numbers, follow these simple 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 number 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 F# standard library  
  3. open System  
  4. //specifies the location of the Drawing classes  
  5. open System.Drawing  
  6. //specifies the namespace memory location of the form class  
  7. open System.Windows.Forms  
  8. //creates a new form  
  9. let addform=new Form(Text="Add Numbers", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a label  
  11. let n1label=new Label(Text="First number:",Top=20,Left=5,AutoSize=true)  
  12. let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. //creates another label and change its text to “Second number:”  
  14. let n2label=new Label(Text="Second number:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  15. let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50))  
  16. //creates another label and change its text to sum  
  17. let n3label=new Label(Text="Sum:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  18. //creates a label that will display the result of the computation  
  19. let anslabel=new Label(Location=new System.Drawing.Point(80, 90))  
  20. //make our buttons  
  21. let addbutton=new Button(Text="Add", Location=new System.Drawing.Point(100, 130))  
  22. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  23. //add the controls into the form  
  24. addform.Controls.Add(n1label)  
  25. addform.Controls.Add(firsttextbox)  
  26. addform.Controls.Add(n2label)  
  27. addform.Controls.Add(secondtextbox)  
  28. addform.Controls.Add(n3label)  
  29. addform.Controls.Add(anslabel)  
  30. addform.Controls.Add(addbutton)  
  31. addform.Controls.Add(exitbutton)  
  32.   
  33. //when the add button is clicked  
  34. addbutton.Click.Add(fun addfunction ->  
  35. //convert the textbox values to unsigned int  
  36. let firstnum=Convert.ToUInt32(firsttextbox.Text)  
  37. let secondnum=Convert.ToUInt32(secondtextbox.Text)  
  38. let sumval=firstnum + secondnum  
  39. //display the sum in the anslabel  
  40. anslabel.Text<-Convert.ToString(sumval))  
  41.  //when the exit button is clicked, close the form              
  42. exitbutton.Click.Add(fun exit -> addform.Close())    
  43. addform.Show()  
  44. Application.Run(addform)  

6. Press Ctrl+F5 or click the run icon to execute your application. You should now see something similar to the screenshot below:



7. That's all. Enjoy programming!

Fixing the “Fatal Error: Call to undefined imagecreate() function” in PHP

The "call to undefined imagecreate() function" normally appears if you try to execute graphic related scripts such as CAPTCHA scripts. To fix this problem, follow these simple steps:

1. Download a copy of PHP zip package from http://php.net/downloads.php.

2. For the sake of example, we will be using PHP 5.2.14 Windows Binary Zip Package.

3. Once you have downloaded the file, Right-Click it>Extract files>In the Winrar Destination path, enter “C:\PHP\” no quotes. If you have previously installed PHP distros just overwrite it.(I’ve assumed here that you have Winrar installed on your computer.)

4. Go to where your PHP file was extracted, in this case in the C:\ directory.

5. Locate php.ini-dist and rename it to php.ini.(Overwite the previous php.ini).



6. Right-click php.ini>Select Open. The following should then appear:



7. Click Edit>Select Find then enter extension_dir in the Find what textbox.



8. Change the line extension_dir=”./” to extension_dir=”C:\PHP\ext”.



9. Click Edit>Find>Enter “gd”(no quotes) in the Find what textbox then click Find Next.



10. Delete the semi-colon(;) before the line ;extension=php_gd2.dll.




11. Click File the Save. You should now be able to execute your captcha scripts without errors. Here is the screenshot of my captcha that I was able to execute effortlessly using these fixes:



12. If you continue to encounter error messages, try restarting your computer.

13. That's all. Ciao!

Changing the text color of an F# Console Application

By default, console texts are displayed in white. To change the text color, use the Console object together with its ForegroundColor Property.

  1. Syntax:  
  2. Console.ForegroundColor<-Color value  
Color values are stored in the ConsoleColor object and has the following values:

1. Black
2. DarkBlue
3. DarkGreen
4. DarkCyan
5. DarkRed
6. DarkMagenta
7. DarkYellow

8. Gray
9. DarkGray
10. Cyan
11. Red
12. Yellow
13. White
 

To assign the ConsoleColor object as a value of the Console ForegroundColor property, use the following syntax:
  1. Console.ForegroundColor<-ConsoleColor.Colorname  
For the sake of example, let us make a console application that will display the text “I am cyan” in cyan text color by following 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. The following should then come into view: 4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. //Use the F# Library  
  2. open System  
  3. //assigns Cyan color to our console text color  
  4. Console.ForegroundColor<-ConsoleColor.Cyan  
  5. //Create a function named myfunction()  
  6. let myfunction()=  
  7. //adds an action to our function  
  8. //in this case, displays an "I am Cyan" text  
  9. printfn "I am Cyan"  
  10. //executes our function          
  11. myfunction()  
5. Press Ctrl+F5 to execute the console application. You should now see the following output:
6. To add a background color, you can use the BackgroundColor property of the Console object which follows the same syntax as ForegroundColor property. That’s all. Thanks!