Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RX AutoCompleteBox

I'm trying to build an filter control using RX and WPF. So I have a textbox and a listbox. On start up the listbox has 100 contact names and the user can type in a name to filter the list.

Question is how can I build up an text stream (key inputs) and then publish. This should be Time sensitive so I guess only after 750milliseconds if a key input hasnt been detected then the filter may be performed.

Thanks

like image 759
Andy Avatar asked Dec 27 '22 23:12

Andy


1 Answers

The basic outline would looks like so

  1. textbox keydown event converted to an IO
  2. throttling of the keystrokes, so that we don't search while the user is actually typing
  3. Do the search
  4. Place the the search results onto the list box

Here's some pseudo-code -

 var keysIO =   Observable.FromEvent<KeyDownEventHandler, RoutedEventArgs>(
                                    h => new KeyDownEventHandler(h),
                                    h => btn.KeyDown += h,
                                    h => btn.KeyDown -= h));

 var searchResults = keysIO.Throttle(TimeSpan.FromSeconds(0.750),Scheduler.Dispatcher);

 searchResults.Subscribe(sr => {  lb.Clear(); lb.AddRange(sr); });

@Andy, Throttle won't kick off a search every 750ms, only after the user has stopped typing for 750ms. Try this in LinqPad.

   Observable.Interval(TimeSpan.FromMilliseconds(10))
   .Do(ii =>  "keystroke".Dump())
   .Take(10)
   .Throttle(TimeSpan.FromSeconds(0.750))
   .Select(ttl => "search")
like image 127
Scott Weinstein Avatar answered Dec 30 '22 12:12

Scott Weinstein