Partager via


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

Returns a new event that listens to the original event and triggers the resulting event only when the argument to the event passes the given function.

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

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

// Signature:
Event.filter : ('T -> bool) -> IEvent<'Del,'T> -> IEvent<'T> (requires delegate)

// Usage:
Event.filter predicate sourceEvent

Parameters

  • predicate
    Type: 'T ->bool

    The function to determine which triggers from the event to propagate.

  • sourceEvent
    Type: IEvent<'Del,'T>

    The input event.

Return Value

An event that only passes values that pass the predicate.

Remarks

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

Example

The following code example shows how to use the Event.filter function. In this example, mouse events are passed on only when the mouse pointer is in a certain region.

let form = new Form(Text = "F# Windows Form",
                    Visible = true,
                    TopMost = true)
form.MouseMove
    |> Event.filter ( fun evArgs -> evArgs.X > 100 && evArgs.Y > 100)
    |> Event.add ( fun evArgs ->
        form.BackColor <- System.Drawing.Color.FromArgb(
            evArgs.X, evArgs.Y, evArgs.X ^^^ evArgs.Y) )

Platforms

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

Reference

Control.Event Module (F#)

Microsoft.FSharp.Control Namespace (F#)