Reader Level:
ARTICLE

Working With Forms Classes in F#

Posted by Dea Saddler Articles | F# August 03, 2011
This article is a demonstration regarding Forms classes in F#. Take a quick review to learn.
  • 0
  • 0
  • 3423
Download Files:
 

Introduction

Generally you use Script Style Programming, in which you create a Form and add controls to it. This way of programming is mainly used and beneficial for single form applications. If you want to create an application composed of multiple forms then Script style programming has limitations for that and you also want to create libraries of forms for use with other .Net languages. In all these situations you should follow a more component oriented approach.

Here we are discussing that if you want to use a Form repeatedly while creating a large WinForms application and want these Forms to be able to communicate with each other by adjusting each other's properties and calling each other's methods. You can do this by defining a new form class that drives from System.Windows.Forms. We are also using Class syntax.

open System.Windows.Forms

Class

In Object Oriented Programming a class should model some concept used within the program or library you are creating like a process class models operating system process and the string class models a collection of characters.

A class can be defined like a Type so a class definition begins with a Type keyword followed by the name of the class and the parameters of the class's constructors with parentheses then we put a equal sign followed by a class's member definitions. The most basic member of class is called a Method, which is a function that accesses the parameter of the class.

Getting Started

Here we are discussing an example in which we create a Form with three fields that are a Label, Button and TextBox. Then we manipulate the fields using external code. Finally we will create a new instance of this form and then set the Text Property of the TextBox field.

Step 1: First open a new project in F# using Visual Studio 2010 and give a name to it.

New Project Dialog Box

Step 2: Click on Program.fs file in Solution Explorer.

Solution Explorer

Step 3: Write the following code in the Program.fs window. Your window will look like below.

Form Classes Example
open System
open System.Windows.Forms
// a class that derives from "Form" and add some user controls

type
MyForm() as a =
     inherit Form(Width=200, Height=200)
// create some controls to add the form
     let lbl = new Label(Top=8, Left=8, Width=40, Text="Input:")
     let txtbx = new TextBox(Top=8, Left=48, Width=40)
     let btn = new Button(Top=8, Left=96, Width=60, Text="Click!")
// add a event to the button
     do btn.Click.Add(fun _ ->
        let form = new MyForm(Text=txtbx.Text)
        form.Show())
// add the controls to the form
        do a.Controls.Add(lbl)
        do a.Controls.Add(txtbx)
        do a.Controls.Add(btn)
// expose the text box as a property
     member x.Textbox = txtbx
// create an new instance of our form
let form =
        let temp = new MyForm(Text="Form Classes")
        temp.Textbox.Text <- "Next!"
        temp
[<STAThread>]
do Application.Run(form)

Step 4: Now press F5 to execute the code.

Output

Form Classes output1

Form Classes Output2

Summary

In this article I have Discussed about Forms Classes in F#.

COMMENT USING

Trending up