0

Is it possible, or more precisely how is it possible to use RX.Net to listen to a number and different variety of (WinForms) controls' .TextChanged/.RowsChanged/.SelectionChanged events and whenever one condition is fullfilled (ControlA.Text isn't empty, ControlB.RowsCount > 0 etc) enable that one DoSomething button.

I am asking because currently we have a lengthy if/then statement in each of these events' handlers and maintaining them if the condition changes is, due to duplicate code, quite error prone and that's why, if possible, I think it would be nice to take the stream of events and put the condition in one place.

Has anyone done that?

Jörg Battermann
  • 4,044
  • 5
  • 42
  • 79
  • Using RX is really overkill. And you should not do this in the view itself. It is much much easier to have a **viewmodel** with properties like `CanDoSomething` that you databind with the `Enabled` property of the button. I haven't got time now to elaborate on this. Maybe later, or someone else? – Gert Arnold May 05 '13 at 13:02
  • @GertArnold thanks but this is a WinForms application with no separation between Model/View/ViewModel. There are certainly other solutions and workarounds but from what I understand combining different Events' streams and doing something if exactly one condition is fulfilled at a moment in time seems like a natural fit for RX, hence the Question.. whether or not this works, and either way why (not) and ideally how so. – Jörg Battermann May 05 '13 at 17:50

2 Answers2

0

You should be able to use .FromEventPattern() to create observables for each of those events and then use CombineLatest() to do your logic on the current overall state and determine whether your button should be enabled, in one place.

Tim
  • 960
  • 5
  • 19
0

You can use Observable.FromEventPattern in combination with Join patterns, with Observable.When, Observable.And and Observable.Then, to create observables which will fire depending on various conditions, like combinations of events. For example, consider my answer here: Reactive Extensions for .NET (Rx): Take action once all events are completed

Community
  • 1
  • 1
Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62