Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there is a fluent approach to handling WinForm event?

I was handling yet another KeyDown event in a user control when I wondered if it existed a library for typing fluent code for handling event like

editor.When(Keys.F).IsDown().With(Keys.Control).Do((sender, e) => ShowFindWindow());

Does that exist?

like image 215
Pierre-Alain Vigeant Avatar asked Oct 30 '09 15:10

Pierre-Alain Vigeant


2 Answers

I downloaded the Reactive Extension Framework for the .Net Framework 3.5 SP1.

I was able to do the same:

Observable.FromEvent<KeyEventArgs>(this, "KeyDown")
    .Where(e => e.EventArgs.KeyCode == Keys.F && e.EventArgs.Modifiers == Keys.Control)
    .Subscribe(e => MessageBox.Show(e.EventArgs.KeyData.ToString()));

This is some pretty powerful stuff.

like image 189
Pierre-Alain Vigeant Avatar answered Nov 12 '22 09:11

Pierre-Alain Vigeant


.NET 4 will introduce the reactive framework (IObservable<T>, IObserver<T> and helper extension types) which should offer exactly this.

like image 2
Richard Avatar answered Nov 12 '22 09:11

Richard