TextBox And Button Control in F#

Introduction

Today I am going to explain F# Textbox and Button controls by implementing a Book Song example. The Book Song example will show the song for 60 books until zero book. Both the controls TextBox and Button are used in the example. First we have created a form then we added controls to the Form.

TextBox

A TextBox control is used to display information entered either by you/user at design time or by an assignment statement in the code. A TextBox purpose is to allow the user to input Text to be used by the program. We use a single line text box or set the property to single line when only one line of input is required and set the property to multiline when more than one line of input is required.

Some TextBoxes are not editable; they are only used for displaying text. A TextBox is based on custom logic rules. In GUI programming, you will handle custom events provided by the TextBox to know when to execute your logic rules. The text displayed by a TextBox can be accessed using the TextBox Text property. A TextBox can contain zero, one or two scrollbars.

Button

Buttons are mainly used to start, end or interrupt a process. A button can be pressed either by clicking it by a mouse or by tabbing to it and than pressing enter. The Button is mainly used to initiate action. The caption Property determines the text to display on the face on the Button. If we set the Cancel property of the Button to true the Button will be activated from anywhere in the form by pressing the Esc key.

We can also invoke a Button's click event by setting up an "access key" for it. From the user point of view a Button is useful when clicked, in which case the user positions the mouse on it and presses one of the mouse buttons. The classic button is called a Command Button. A Button needs a container or host. The Container could be a form, a toolbar etc.
Properties of TextBox

Properties Description
BackColor Get/set the background color of the control.
ContextMenu Get/set the shortcut menu associated with the control
Height Get/set the height of the control
Margin Get/set the space between control
Name Get/set the name of the control
Site Get/set site of the control
Text Get/set the current Text in the TextBox

Properties Of Button
Properties Description
Name Get/set the name of the Button
Site Get/set the site of the Button
Width Get/set the width of the Button
ForeColor Get/set the foreground color of the control
BackColor Get/set the background color of the control
Font Get/set the font of the Text displayed by the control
Size Get/set the height and width of the control

Getting Started
Step1- Open a new project in F# using Visual Studio 2010 and give a name to it.
New Project dialog Box

Step2- Click on Program.fs file in Solution Explorer.
Solution Explorer

Step3- Write the following code in the Program.fs window; your window will look like below.
Example for TextBox and Button

#light

open
System
open System.Text
open System.Drawing
open System.Windows.Forms

let
BookSong =
let strbSong = new StringBuilder()
let append (s:string) = strbSong.Append(s) |> ignore
for a = 60 downto 0 do
if (a = 0) then
append("\nNo more book of F# on the library, no more book of F#." +
"\nGo to the store and buy some more, 60 book of F# on the library.")
else
let n = a - 1
let plural = if (a = 1) then "" else "s"
append (sprintf "\n%d book%s of F# on the library, %d book%s" a plural a plural);
append "\nTake one down and pass it around, ";
match n with
| 1 -> append "1 book "
| 0 -> append " no more book "
| _ -> append (sprintf "%d book of F# on the library.\n" n)
strbSong.ToString()
let form = new Form(Width=500, Height=420,
Visible=true, TopMost=true,
FormBorderStyle=FormBorderStyle.FixedSingle,
Text="Book counting Song")
let txtBox = new RichTextBox(Size=new Size(480, 350),
Location=new Point(5, 5),
Text=BookSong)
form.Controls.Add(txtBox)
let btnExit = new Button(Location=new Point(408, 360), Text="Exit")
btnExit.Click.Add (fun _ -> form.Close())
form.Controls.Add(btnExit)

[<STAThread>]
do Application.Run(form)
Step4- Now press F5 to execute the code.
Output
Example TextBox, Button Output1
Example TextBox, Button Output2

Summary
In this article I have discussed the TextBox and Button controls in F#.

Up Next
    Ebook Download
    View all
    Learn
    View all