This sample is lift from my forth coming book on F#, you will need entlib and the AdventureWorks example db to make it work properly, but you can probably remove both these depenacies relitavely easily. You can preorder the book here. Actually this sample has not been tech reviewed yet, so there maybe some small changes in the final version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#light
#r "Microsoft.Practices.EnterpriseLibrary.Data.dll";;
open System
open System.Collections.Generic
open System.Data
open System.Windows.Forms
open Microsoft.Practices.EnterpriseLibrary.Data


let execCommand (commandString : string) =
    let opener() =
        let database = DatabaseFactory.CreateDatabase()
        database.ExecuteReader(
            CommandType.Text, 
            commandString)

    let generator (reader : IDataReader) =
        if reader.Read() then
            let dict = new Dictionary<string, obj>()
            for x = 0 to (reader.FieldCount - 1) do
                dict.Add(reader.GetName(x), reader.Item(x))
            Some(dict)
        else
            None

    IEnumerable.generate_using
        opener
        generator

let contactsTable = 
    execCommand 
        "select top 10 * from Person.Contact" 

let contacts = 
    contactsTable
    |> IEnumerable.map
        (fun row -> 
            Printf.sprintf "%O %O"
                (row.Item("FirstName"))
                (row.Item("LastName")))
    |> IEnumerable.to_array

let form =
    let temp = new Form()
    let combo = new ComboBox()
    combo.DataSource <- contacts
    combo.Top <- 8
    combo.Left <- 8
    temp.Controls.Add(combo)
    temp

Application.Run(form)
By on 12/3/2006 4:29 AM ()

(* New Form -> Form1 *)
let form = new Form() in
form.Visible <- true;
form.Text <- "Hello World";

(* New Form -> Form2 *)
let form2 = new Form() in
form2.Visible <- false;
form2.Text <- "Hello RIo de Janeiro";

(* New Button *)
let btn = new Button() in
btn.Dock <- DockStyle.Fill;
btn.Font <- new Font("Arial", Float32.of_float 20.0);
btn.Text <- "Click me!";
form.Controls.Add btn;

(* Event Button_Click *)
btn.add_Click(new EventHandler (
fun _ _ -> form2.Show() ));

Application.Run(form);

The problem which this code is:
when i close the form2, and click again in button on form1, i recive this error message:

Cannot access a disposed object.
Object name: 'Form'.

I need this Dispose method, like c# have?

By on 12/3/2006 7:35 AM ()

I think that since form2 is static, when you close it, it is disposed and thus not accessible any more.

So you have two options, have a method that creates Form2 :

1
2
3
4
5
6
7
8
let  form2 () = 
  let f = new Form() in
  f.Visible <- false;
  f.Text <- "Hello RIo de Janeiro";
  f

btn.add_Click(new EventHandler (
        fun _ _ -> form2() ))

Or overriding the closing event (haven't tested it though):

1
2
3
4
5
let  form2 = new Form() 
let _ =  
  form2.Visible <- false ;
  form2.Text <- "Hello RIo de Janeiro";
  form2.Closing.Add( fun e -> form2.Visible <- false ; e.Cancel <- true)
By on 12/3/2006 11:18 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper