You need to use the .Add(<event here>) method of a control's event. Here's some demo code. I haven't tested the cell click part of it though. I hope it's self-explanatory enough.

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
open System.Windows.Forms
open System


let frm = new Form()
let grid = new DataGridView(Dock = DockStyle.Fill, Visible=true)


//Add an anonymous method to handle an event
grid.Click.Add(fun e -> printfn "Event: %A" e)


//Cast the event to MouseEventArgs where applicable
grid.Click.Add(fun e -> printfn "Button: %A" (e:?>MouseEventArgs).Button)


//Declare a handler explicitly to be able to remove it later (can't do that with anonymous handlers)
let clickHandler = new System.EventHandler(fun sender args -> printfn "Sender: %A, args: %A" sender args)
grid.Click.AddHandler(clickHandler)


//Uncomment the line below to remove clickHandler
//grid.Click.RemoveHandler(clickHandler)


//You can even filter events
let rightClickOnlyEvent = 
    grid.Click |> Event.filter (fun e -> (e:?>MouseEventArgs).Button = MouseButtons.Right)    

rightClickOnlyEvent.Add(fun _ -> printfn "I only listen to right clicks.")


//warning: code below untested
//handling CellClick
grid.CellClick.Add(fun e -> printfn "Row: %d, Col: %d" e.RowIndex e.ColumnIndex)


//A function that builds an event that listens only to a specific column click
let specificColumnEvent colIndex =
    grid.CellClick |> Event.filter (fun ce -> ce.ColumnIndex = colIndex)


//now listen to clicks on the second column only
(specificColumnEvent 1).Add(fun _ -> printfn "Second column clicked!")


frm.Controls.Add(grid)
frm.Show()
By on 1/3/2010 4:44 AM ()

Fantastic! Thanks for your response. This is exactly what I was looking, now my table responds to click events.

As a next step in the F# learning process, the demo I was working from gets data from yahoo finance (date, open, high, low, close) and shows the data in a grid. In the grid's click event handler, I would like to send the date (first cell in the row) to all other tables and have all other tables highlight their row containing that date and make the row visible (if that date exists)

I was trying to follow Don Syme's posting, [link:blogs.msdn.com] from there it looks like i need some sort of command process on to which all grids get registered. But, i'm still struggling with the F# syntax. Any thoughts??

Many thanks!

Roberto

By on 1/3/2010 12:43 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