<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Gideon Dsouza - winforms</title><link>http://www.gideondsouza.com:80/Tags/winforms</link><description>Gideon Dsouza - winforms</description><item><title>Implementing simple instant search with Rx (Reactive Extensions)</title><link>http://www.gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</link><description>&lt;p&gt;Haven't heard of Rx, this could be a little starter tutorial for you.&lt;/p&gt;

&lt;p&gt;Rx or Microsoft's &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609"&gt;Reactive extensions &lt;/a&gt;makes life simple when it comes to asychronous programming&lt;/p&gt;

&lt;p&gt;My most favorite thing about Rx is how simple it makes &lt;em&gt;instant&lt;/em&gt; search! 
A while ago I &lt;a href="http://stackoverflow.com/questions/4278069/implement-instant-threaded-search-algorithm"&gt;asked around on stackoverflow&lt;/a&gt; and just architecturing it was so difficult.&lt;/p&gt;

&lt;p&gt;So I'm going to go through a dead simple example, where we're going to have an &lt;strong&gt;instant search&lt;/strong&gt;, but, we'll have some dummy data in memory that we're going to search.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; : I wrote a continuation of this article &lt;a href="http://gideondsouza.com/blog/abstracting-reactive-extensions-for-sql-server-compact-and-implementing-an-instant-search"&gt;here&lt;/a&gt; where I describe instant search with Rx on a SqlCE database and a nice clean abstraction.&lt;/p&gt;

&lt;p&gt;Here's what it'll look like:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/appCapture.JPG" alt="Instant Search App"&gt;&lt;/p&gt;

&lt;p&gt;I hope to do a post on searching real data, maybe with sql-compact 4, and then I'll get on to how to write &lt;a href="http://stackoverflow.com/questions/5349140/iasyncrepository-or-iobservablerepository-for-silverlight-4-wcf-data-services"&gt;Observable Repositories&lt;/a&gt; which makes life easier, especially for Silverlight!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How To:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So head over to Visual Studio and create a new form (or you can &lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;download the sample&lt;/a&gt;). Add a textbox and listbox like the screenshot.&lt;/p&gt;

&lt;p&gt;We need to install &lt;a href="http://nuget.org/List/Packages/Rx-Main"&gt;Rx-Main&lt;/a&gt; and &lt;a href="http://nuget.org/List/Packages/Rx-WinForms"&gt;Rx-WinForms&lt;/a&gt; from nuget.&lt;/p&gt;

&lt;p&gt;You can right click your solution and choose &lt;code&gt;Manage Nuget Packages..&lt;/code&gt; and search and install these.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://www.gideondsouza.com/Media/Default/BlogPost/blog/nugetScrenie.JPG" alt="Visual Studio Package Manager"&gt;&lt;/p&gt;

&lt;p&gt;This is the code then:&lt;/p&gt;

&lt;script src="https://gist.github.com/1422982.js"&gt; &lt;/script&gt;

&lt;p&gt;&lt;strong&gt;What/How/Why?&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;The starting point to observables is the &lt;a href="http://msdn.microsoft.com/en-us/library/hh244252(v=VS.103).aspx"&gt;Observable&lt;/a&gt; class, the &lt;a href="http://msdn.microsoft.com/en-us/library/hh229251(v=VS.103).aspx"&gt;FromEventPattern&lt;/a&gt; method creates an observable sequence from the &lt;code&gt;TextChanged&lt;/code&gt; event of the textbox. &lt;/p&gt;

&lt;p&gt;Think of this like a list of notifications coming to you. &lt;/p&gt;

&lt;p&gt;We then chain a method call to &lt;a href="http://msdn.microsoft.com/en-us/library/hh229400(v=VS.103).aspx"&gt;Throttle&lt;/a&gt; (my fav part!), it well, throttles this list of notifications by 500 milliseconds. &lt;/p&gt;

&lt;p&gt;Here, for win-forms you need the &lt;a href="http://msdn.microsoft.com/en-us/library/hh212067(v=VS.103).aspx"&gt;ControlScheduler&lt;/a&gt; class otherwise the throttling doesn't work too well. This class comes from the Rx-WinForms package we added earlier. The &lt;a href="http://msdn.microsoft.com/en-us/library/hh228941(v=VS.103).aspx"&gt;Do&lt;/a&gt; method just does the metho you want to run, and you send off the text from the the textbox over. Lastly, calling Subscribe starts off the notifications.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.gideondsouza.com/Media/Default/Misc/BasicRxSearchClean.zip"&gt;Download the Sample here.&lt;/a&gt;(1.3MB) It's a clean solution but with the Rx nuget packages.&lt;/p&gt;

&lt;p&gt;Hope this was useful!&lt;/p&gt;
</description><pubDate>Thu, 17 Nov 2011 19:21:00 GMT</pubDate><guid isPermaLink="true">http://www.gideondsouza.com:80/blog/implementing-simple-instant-search-with-rx-reactive-extensions</guid></item></channel></rss>