Geeks With Blogs
Mark Pearl

 

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()
Posted on Wednesday, June 9, 2010 9:45 PM F# | Back to top


Comments on this post: Simple Mouse Move Event in F# with Winforms

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
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
Left by Art Scott on Sep 23, 2010 10:49 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
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.
Left by Kale Kapı on Feb 19, 2011 12:23 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
if you reduce the size it works then with the mous. The Xy cordinates are not possible to change.
Left by Efient on May 23, 2011 5:25 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
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
Left by Çelik Kapı on Oct 28, 2011 11:00 AM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
I was wondering could we write the x y coordinates of the mouse at header while we move mouse.

how can we do that.
Left by çelik kapı on Oct 28, 2011 1:01 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
I was wondering could we write the x y coordinates of the mouse at header while we move mouse.
Left by çelik kapı on Nov 17, 2011 3:54 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
Hi Mark.
Thanks for this post.
i solved a big problem with this.
Left by Çelik Kasa on Feb 03, 2012 4:22 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
great to hear that!
Left by Mark Pearl on Feb 03, 2012 4:23 PM

# re: Simple Mouse Move Event in F# with Winforms
Requesting Gravatar...
if i tried to renew the x and y coordinates then the mouse over does not work correctly. Can someone help me please
Left by çelik kapı on Mar 19, 2012 12:10 PM

Your comment:
 (will show your gravatar)


Copyright © MarkPearl | Powered by: GeeksWithBlogs.net