A Responsive Windows Forms UI using Reactive Extensions

For WPF programming there is a Reactive Extensions extension called ReactiveUI. I like the idea very much, but the last time I have looked at the online documentation, it was slightly out of sync with the current version, and the examples are slightly more elaborate than hello world. I’ll have to return to ReactiveUI at a later point in time.

As it seems, many people still use WinForms. A typical Forms application contains quite a number of event handlers. What Rx promises is a declarative design of your program (UI) logic. Without getting too concerned with lifetime management of this hello world example, here it is:

Responsive WinForms using Rx

The first TextBox gets updated asynchronously with the current time, the second TextBox shows the word count of the text entered in the third TextBox. All that is defined in a single Load event handler, private void Form1_Load(…). You can get the code here.

The initialization of the logic (in Form1_Load) is as follows:

Update textBox1 with the current time asynchronously every second

Observable.Interval(TimeSpan.FromSeconds(1))
          .ObserveOn(this) // scheduled on the Form's scheduler
          .Subscribe(_ => textBox1.Text = DateTime.Now.ToLongTimeString());

No threading code needed :).

Convert an event handler into an observable and set its registration

var textChanged = Observable.FromEventPattern<EventHandler, EventArgs>
 (
  handler => handler.Invoke,
  h => textBox3.TextChanged+= h,
  h => textBox3.TextChanged-= h
 );

Subscribe to the textChanged observable in order to update the word count

textChanged
    .ObserveOn(this) // scheduled on the Form's scheduler
    .Subscribe(x => textBox2.Text =
        textBox3.Text
        .Split()
        .DefaultIfEmpty()
        .Where(s=>s.Trim().Length>0)
        .Count()
        .ToString());

That’s it. No other explicit event handlers, just these observables with lambdas.

Continued here….

Update: Since the original post, a lot has changed. ReactiveUI has a shiny new docs site, a lot of API improvements, and improved exposure.

Yet another WordPress website – s-pb.info

Quite a long time ago I fell in love with my home town anew. The feeling could be summarized in the following picture
bridgefog_spb
After taking the photo I thought of creating a website for people wanting to visit the city – to create a pragmatic tour guide and let people share their experiences. At that time I didn’t know of the existence of wordpress, and I suppose, it wasn’t as stable or usable as it is today. I’ve registered it on July 18, 2006, and made a simple static page, which I’ve mirrored in today’s update. It has remained unchanged since.

I’ll give it a new life and let google and spam bots run over it. Let’s see where the road leads. As usual, feel free to register and post your experiences. I have some ideas, for example, to explain the current foreigner registration procedure, but I’m not sure when I’ll be able to write it up.

Until then:

http://s-pb.info

My Fourth WordPress Site

It’s a great feeling finding a technology one can be comfortable with and enjoy using it. WordPress has shown itself from its best sides. I nearly never had to edit a php file, with most tasks accomplished using a range of plugins. Even moving from a development server to a new one using the Duplicator plugin worked almost out of the box: the security features I have set up did not work well with that, and some manual work was required. With that said and done, please welcome the new WordPress self-hosted website: pixyshoes.de

pixyshoes.de screenshot

I cannot claim now to have designed it, since we’ve used the Oxygen theme, and Corina is managing the content, as well as the design, as far as the customization options allow.

However, some product photos are still made by me, as well as the cover photo:

Alina photographed by DLed

The other websites I have created:

No need to switch to Drupal, Joomla or anything else in the nearest future. Perhaps, my next website will be a WordPress website again.