Table of contents
Collapse the table of content
Expand the table of content

Event.choose<'T,'U,'Del> Function (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

Returns a new event which fires on a selection of messages from the original event. The selection function takes an original message to an optional new message.

Namespace/Module Path: Microsoft.FSharp.Control.Event

Assembly: FSharp.Core (in FSharp.Core.dll)


// Signature:
Event.choose : ('T -> 'U option) -> IEvent<'Del,'T> -> IEvent<'U> (requires delegate)

// Usage:
Event.choose chooser sourceEvent


chooser Type: 'T -> 'Uoption

The function to select and transform event values to pass on.

sourceEvent Type: IEvent<'Del,'T>

The input event.

Return Value

An event that fires only when the chooser returns Some.


This function is named Choose in compiled assemblies. If you are accessing the function from a .NET language other than F#, or through reflection, use this name.


In this example, the function is used to select only events when the mouse button is down. At the same time, the function transforms the input data of type MouseEventArgs into a more convenient format, a tuple of two integers that represent the current mouse position.

// When the mouse button is down, the form changes color
// as the mouse pointer is moved.

let form = new Form(Text = "F# Windows Form",
                    Visible = true,
                    TopMost = true)
    |> Event.choose(fun evArgs ->
        if (evArgs.Button <> MouseButtons.None) then
            Some( evArgs.X, evArgs.Y)
        else None)

    |> Event.add ( fun (x, y) ->
        form.BackColor <- System.Drawing.Color.FromArgb(
            x, y, x ^^^ y) )


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Control.Event Module (F#)

Microsoft.FSharp.Control Namespace (F#)

© 2020 Microsoft