posts - 293 , comments - 411 , trackbacks - 0

My Links

News

View Steve Michelotti's profile on LinkedIn

Twitter












Tag Cloud

Archives

Post Categories

My Online Presence

Simple Mouse Move Event in F# with Winforms

 

This evening I had the pleasure of reading one of ThomasP’s blog posts on first class events. It was an excellent read, and I thought I would make a brief derivative of his post to explore some of the basics.

In Thomas’s post he has a form with an ellipse on it that when he clicks on the ellipse it pops up a message box with the button clicked… awesome. Something that got me on the post though was the code similar to the one below…

// React to Mouse Move events on the form
let evtMessages =
    frm.MouseMove      
     |> Event.map (fun mi ->                                
        mi.Location.ToString())
     |> Event.map (sprintf "Hey, you clicked on the ellipse.\nUsing: %s")
     |> Event.add (MessageBox.Show >> ignore)

The MessageBox is a function with a string passed into it. What if I wanted to rather change a mutable value holder instead, how would the syntax go for that? Immediately the thought came to me of anonymous functions. I’ve used them before to do something like this…

let HelloPerson personName = 
    "Hello " + personName |> fun(x) -> Console.WriteLine(x)    

So using the same approach I adapted the event code to instead of showing a Message Box with a string passed in to it, to rather change the forms header.

|> Event.map (sprintf "Your mouse position is %s")
|> Event.add(fun(x) -> frm.Text <- x)

Okay… it looks a bit weird with the –> x <- syntax, but makes sense and works… The next thing I wanted to do was change Thomas’s code sample from having an ellipse, and reacting to the position of the mouse and click, to rather trigger the event whenever the mouse moved. This simple involved removing some filtering code.

Finally I wanted the code to work as a FSharp Project without having to run through the F# interactive. To achieve this I just needed to find out how to trigger the window event loop. This can be achieved with the code below…

// Program eventloop
while frm.Created do
    Application.DoEvents()

 

So lets look at the complete code sample…

#light
open System
open System.Drawing
open System.Windows.Forms

// Create the main form
let frm = new Form(ClientSize=Size(600,400))

// React to Mouse Move events on the form
let evtMessages =
    frm.MouseMove      
     |> Event.map (fun mi ->                                
        mi.Location.ToString())     
     |> Event.map (sprintf "Your mouse position is %s")
     |> Event.add(fun(x) -> frm.Text <- x)

// Show the form
frm.Show()

// Program eventloop
while frm.Created do
    Application.DoEvents()

Print | posted on Wednesday, June 9, 2010 9:45 PM | Filed Under [ F# ]

Feedback

Gravatar

# re: Simple Mouse Move Event in F# with Winforms

Hi Mark.
Thanks for this post.

I'm looking for best F# GUI mouse event handling ...
I started with Robert Pickering's Foundations book Listing 8.3, and that lead to may searches ... currently reading Petricek and Syme's "Collecting Hollywood's Garbage", "Avoiding Space-Leaks in Composite Events" (gr8 title) ...

PLH OOE Art
9/23/2010 10:49 PM | Art Scott
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

You adapted the event code to change the forms header.

I was wondering could we write the x y coordinates of the mouse at header while we move mouse.

how can we do that.
2/19/2011 12:23 PM | Kale Kapı
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

if you reduce the size it works then with the mous. The Xy cordinates are not possible to change.
5/23/2011 5:25 PM | Efient
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

I'm looking for best F# GUI mouse event handling ...
I started with Robert Pickering's Foundations book Listing 8.3, and that lead to may searches ... currently reading Petricek and Syme's "Collecting Hollywood's Garbage", "Avoiding Space-Leaks in Composite Events" (gr8 title) ...

PLH OOE Art
10/28/2011 11:00 AM | Çelik Kapı
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

I was wondering could we write the x y coordinates of the mouse at header while we move mouse.

how can we do that.
10/28/2011 1:01 PM | çelik kapı
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

I was wondering could we write the x y coordinates of the mouse at header while we move mouse.
11/17/2011 3:54 PM | çelik kapı
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

Hi Mark.
Thanks for this post.
i solved a big problem with this.
2/3/2012 4:22 PM | Çelik Kasa
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

great to hear that!
2/3/2012 4:23 PM | Mark Pearl
Gravatar

# re: Simple Mouse Move Event in F# with Winforms

if i tried to renew the x and y coordinates then the mouse over does not work correctly. Can someone help me please
3/19/2012 12:10 PM | çelik kapı
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: