16 people like it.

Merge two events in one event to move over a list

Merge two events in one event to move over a list. Can be used to implement a wizard / slider mechanism

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


let combine (first:IEvent<'Del,'T>) (second:IEvent<'Del,'T>) (lst:'a list) = 
    let event = new Event<'a>()
    let rec install i =
        let rec next condition op = 
            if condition then
                event.Trigger(lst.[op i 1])
                f.Dispose() 
                s.Dispose()
                install (op i 1)
        and f:IDisposable = first.Subscribe (fun _ -> next (i < lst.Length-1) (+))
        and s:IDisposable = second.Subscribe (fun _ -> next (i > 0) (-))
        ()
    install 0
    event.Publish 

let frm = new Form(Text="Hello world")
let btn = new Button(Text="Button1")
let btn1 = new Button(Text="Button2")
btn |> frm.Controls.Add
btn1 |> frm.Controls.Add
btn1.Top <- 100

[1;2;3;4;5] |> combine btn.Click btn1.Click 
|> Event.add (fun num -> MessageBox.Show (num.ToString()) |> ignore)

frm.ShowDialog() |> ignore
namespace System
namespace System.Windows
namespace System.Windows.Forms
val combine : first:IEvent<'Del,'T> -> second:IEvent<'Del,'T> -> lst:'a list -> IEvent<'a> (requires delegate and 'Del :> Delegate)

Full name: Script.combine
val first : IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)
type IEvent<'T> = IEvent<Handler<'T>,'T>

Full name: Microsoft.FSharp.Control.IEvent<_>
val second : IEvent<'Del,'T> (requires delegate and 'Del :> Delegate)
val lst : 'a list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val event : Event<'a>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val install : (int -> unit)
val i : int
val next : (bool -> (int -> int -> int) -> unit)
val condition : bool
val op : (int -> int -> int)
member Event.Trigger : arg:'T -> unit
val f : IDisposable
IDisposable.Dispose() : unit
val s : IDisposable
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
member IObservable.Subscribe : callback:('T -> unit) -> IDisposable
IObservable.Subscribe(observer: IObserver<'T>) : IDisposable
property List.Length: int
property Event.Publish: IEvent<'a>
val frm : Form

Full name: Script.frm
Multiple items
type Form =
  inherit ContainerControl
  new : unit -> Form
  member AcceptButton : IButtonControl with get, set
  member Activate : unit -> unit
  member ActiveMdiChild : Form
  member AddOwnedForm : ownedForm:Form -> unit
  member AllowTransparency : bool with get, set
  member AutoScale : bool with get, set
  member AutoScaleBaseSize : Size with get, set
  member AutoScroll : bool with get, set
  member AutoSize : bool with get, set
  ...
  nested type ControlCollection

Full name: System.Windows.Forms.Form

--------------------
Form() : unit
namespace System.Text
val btn : Button

Full name: Script.btn
Multiple items
type Button =
  inherit ButtonBase
  new : unit -> Button
  member AutoSizeMode : AutoSizeMode with get, set
  member DialogResult : DialogResult with get, set
  member NotifyDefault : value:bool -> unit
  member PerformClick : unit -> unit
  member ToString : unit -> string
  event DoubleClick : EventHandler
  event MouseDoubleClick : MouseEventHandler

Full name: System.Windows.Forms.Button

--------------------
Button() : unit
val btn1 : Button

Full name: Script.btn1
property Control.Controls: Control.ControlCollection
Control.ControlCollection.Add(value: Control) : unit
property Control.Top: int
event Control.Click: IEvent<EventHandler,EventArgs>
val add : callback:('T -> unit) -> sourceEvent:IEvent<'Del,'T> -> unit (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.add
val num : int
type MessageBox =
  static member Show : text:string -> DialogResult + 20 overloads

Full name: System.Windows.Forms.MessageBox
MessageBox.Show(text: string) : DialogResult
   (+0 other overloads)
MessageBox.Show(owner: IWin32Window, text: string) : DialogResult
   (+0 other overloads)
MessageBox.Show(text: string, caption: string) : DialogResult
   (+0 other overloads)
MessageBox.Show(owner: IWin32Window, text: string, caption: string) : DialogResult
   (+0 other overloads)
MessageBox.Show(text: string, caption: string, buttons: MessageBoxButtons) : DialogResult
   (+0 other overloads)
MessageBox.Show(owner: IWin32Window, text: string, caption: string, buttons: MessageBoxButtons) : DialogResult
   (+0 other overloads)
MessageBox.Show(text: string, caption: string, buttons: MessageBoxButtons, icon: MessageBoxIcon) : DialogResult
   (+0 other overloads)
MessageBox.Show(owner: IWin32Window, text: string, caption: string, buttons: MessageBoxButtons, icon: MessageBoxIcon) : DialogResult
   (+0 other overloads)
MessageBox.Show(text: string, caption: string, buttons: MessageBoxButtons, icon: MessageBoxIcon, defaultButton: MessageBoxDefaultButton) : DialogResult
   (+0 other overloads)
MessageBox.Show(owner: IWin32Window, text: string, caption: string, buttons: MessageBoxButtons, icon: MessageBoxIcon, defaultButton: MessageBoxDefaultButton) : DialogResult
   (+0 other overloads)
Int32.ToString() : string
Int32.ToString(provider: IFormatProvider) : string
Int32.ToString(format: string) : string
Int32.ToString(format: string, provider: IFormatProvider) : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Form.ShowDialog() : DialogResult
Form.ShowDialog(owner: IWin32Window) : DialogResult

More information

Link:http://fssnip.net/2d
Posted:6 years ago
Author:Ankur Dhama
Tags: events