Introduction
Today I am going to explain how to change the color of a Form in a Button click event in a F# Windows Forms application using Visual Studio 2010. This is showing how to create a form and how to register an event handler. When you click the Button the form will change its background color. If you want to build a Windows Forms application in F# you need to install the F# Windows Application (winform) template which you can download from my previous article.
The code of the winform application contains a different section as we are discussing it in Points.
Open
The first four lines of F# program is for import declarations as we usually import "Open System", "Open System.Drawing", "Open System.Windows.Forms" and "Open System.Windows.Forms.Design". The purpose of the Import declarations in F# is that additional namespaces can be provide to F# compiler to search for Type definition.
Example
open System
open System.Drawing
open System.Windows.Forms
open System.Windows.Forms.Design
Let
Let is a Keyword in F#. The Let keyword helps us to bind a name to functions. Here indentation is important, because the let keyword tells the compiler that indented lines belongs to which function. If we use let inside the function this will restrict the scope of that particular association. In the following you can see that here we bind the "Button" with a function that defines the properties of Button.
Example
let form = new Form(Text = "Introducing Button!")
let btn = new Button(Text = "Click to Change Color")
Do
Do is also a Keyword in F#. The code of F# can also run without a Do keyword. This keyword is optional. The Do keyword generally instructs the execution of code.
Example
do btn.Size <- new System.Drawing.Size(130,30)
do btn.Location <- new Drawing.Point(90,100)
(<-)
This symbol is used for the operator named assignment through which we can assign a value to mutable variables. We can not change the value of a variable once it is assigned unless they are defined as mutable variables.
Example
btn.Click.Add(fun _ ->
let rnd = new Random()
let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256)
form.BackColor <- Color.FromArgb(r, g, b) )
Functions
F# program code contains three main function as Application.Run(form), form and controls. Controls can be any control such as label, Button and so on. The indentation tells the compiler which line belongs to which function. Application.Run(form) Is defined in the namespace "System.Windows.Forms". This function is normally called for form creation and execution or run the program.
Example
Application.Run(form)
Now here I am discussing how to create a form that changes color on a Button click Event.
Change Form's Background color on Button Click
Step 1- First open a new F# project Using Visual Studio 2010 and select F# Windows app template and give a name to project.
Step2- In the Solution Explorer click on the Program.fs file.
Step3- Now write the following code in Program.fs. Your Program.fs window will look like below.
namespace Buttoncontrol
open System
open System.Drawing
open System.Windows.Forms
module Main =
// Create form, button and add button to form
let form = new Form(Text = "Introducing Button!")
let btn = new Button(Text = "Click to Change Color")
do btn.Size <- new System.Drawing.Size(130,30)
do btn.Location <- new Drawing.Point(90,100)
form.Controls.Add(btn)
// Register event handler for button click event
btn.Click.Add(fun _ ->
// Generate random color and set it as background
let rnd = new Random()
let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256)
form.BackColor <- Color.FromArgb(r, g, b) )
// Show the form (in F# Interactive)
form.Show()
// Run the application (in compiled application)
Application.Run(form)
Step4- Now press F5 to execute the program.
Output
When you click on Button Background color of the form will change like below.
As in above this form's background can change to many colors.
Summary
In this article I have explained how to create a form that changes its background color in a Button click in a F# Windows Forms application.