In this article, we will continue from the previous article and see a few examples around the UI side. Let us suppose the following snippet, wherein I have simply created one windows form and attached a textbox to it.
- using System;
- using System.Reactive.Linq;
- using System.Windows.Forms;
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- // ObservableGetValue();
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
- Application.Run(form);
- }
- private static void ObservableGetValue()
- {
- IObservable<string> obj = Observable.Generate(
- 0, //Sets the initial value like for loop
- _ => true, //Don't stop till i say so, infinite loop
- i => i + 1, //Increment the counter by 1 everytime
- i => new string('#', i), //Append #
- i => TimeSelector(i)); //delegated this to private method which just calculates time
- //Subscribe here
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
- //Returns TimeSelector
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }


- using System;
- using System.Reactive.Linq;
- using System.Windows.Forms;
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- // ObservableGetValue();
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
- //Trying to fetch the value from textbox
- textbox.TextChanged += textbox_TextChanged;
- Application.Run(form);
- }
- static void textbox_TextChanged(object sender, EventArgs e)
- {
- throw new NotImplementedException();
- }
- private static void ObservableGetValue()
- {
- IObservable<string> obj = Observable.Generate(
- 0, //Sets the initial value like for loop
- _ => true, //Don't stop till i say so, infinite loop
- i => i + 1, //Increment the counter by 1 everytime
- i => new string('#', i), //Append #
- i => TimeSelector(i)); //delegated this to private method which just calculates time
- //Subscribe here
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
- //Returns TimeSelector
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }

- private static void Main(string[] args)
- {
- // ObservableGetValue();
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
- //Using Rx, to get the textchanged
- var textChanged = Observable.FromEventPattern<EventArgs>(textbox, "TextChanged");
- //then, I will subscribe the same
- Application.Run(form);
- }


- using System;
- using System.Reactive.Linq;
- using System.Windows.Forms;
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- // ObservableGetValue();
- GetTextBoxValue();
- }
- private static void GetTextBoxValue()
- {
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
- //Using Rx, to get the textchanged
- var textChanged = Observable.FromEventPattern<EventArgs>(textbox, "TextChanged");
- //then, I will subscribe the same using LINQ Projection
- var query = from e in textChanged
- select ((TextBox) e.Sender).Text;
- //Hence, if the application quits, i want to unsubscribe
- using (query.Subscribe(Console.WriteLine))
- {
- Application.Run(form);
- }
- }
- private static void ObservableGetValue()
- {
- IObservable<string> obj = Observable.Generate(
- 0, //Sets the initial value like for loop
- _ => true, //Don't stop till i say so, infinite loop
- i => i + 1, //Increment the counter by 1 everytime
- i => new string('#', i), //Append #
- i => TimeSelector(i)); //delegated this to private method which just calculates time
- //Subscribe here
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
- //Returns TimeSelector
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }


Santhakumar MunuswamyPosted Oct 18, 2015, 3:40 AM
Good one
Sujeet SumanPosted Oct 12, 2015, 12:20 PM
Nice Article.................
Mohammed IbrahimPosted Oct 12, 2015, 12:09 PM
nice
RakeshPosted Oct 12, 2015, 11:37 AM
Good share buddy
Vipul MalhotraPosted Oct 12, 2015, 10:59 AM
nice article
Sibeesh VenuPosted Oct 12, 2015, 8:27 AM
Nice Share
Mukesh KumarPosted Oct 12, 2015, 8:12 AM
Great, thanks for sharing
Nilesh JadavPosted Oct 12, 2015, 6:06 AM
This is real Great ! Nice work sir !!