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)
- {
-
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
-
- Application.Run(form);
- }
-
-
- private static void ObservableGetValue()
- {
- IObservable<string> obj = Observable.Generate(
- 0,
- _ => true,
- i => i + 1,
- i => new string('#', i),
- i => TimeSelector(i));
-
-
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
-
-
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }
Now, with the above change in place, it will simply give me one window form with one text-box in it.
Now, I would like to listen to text changed event. So, what I would do? I will simply go ahead and attach the text changed handler as in the following code snippet.
Therefore, it has created the following sample event code.
- using System;
- using System.Reactive.Linq;
- using System.Windows.Forms;
-
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
-
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {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,
- _ => true,
- i => i + 1,
- i => new string('#', i),
- i => TimeSelector(i));
-
-
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
-
-
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }
But, if you look at the following screenshot, textchanged is not available.
Hence, this won't be helpful to me. Rather than that, I would prefer to have a reactive extension and subscribe the same as shown below.
- private static void Main(string[] args)
- {
-
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
-
-
-
- var textChanged = Observable.FromEventPattern<EventArgs>(textbox, "TextChanged");
-
-
-
- Application.Run(form);
- }
Now, if you see the following screenshot, it gave me
EventArgs.
Here, in order to get changed text, I need to make use of LINQ on the top of Rx. Hence, below is the finished code for the same. I have done a little bit of refactoring as well, just to keep the code more readable and clean.
- using System;
- using System.Reactive.Linq;
- using System.Windows.Forms;
-
- namespace ReactiveExtensions
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
-
- GetTextBoxValue();
- }
-
- private static void GetTextBoxValue()
- {
- var textbox = new TextBox();
- var form = new Form
- {
- Controls = {textbox}
- };
-
-
-
- var textChanged = Observable.FromEventPattern<EventArgs>(textbox, "TextChanged");
-
-
-
- var query = from e in textChanged
- select ((TextBox) e.Sender).Text;
-
-
- using (query.Subscribe(Console.WriteLine))
- {
- Application.Run(form);
- }
- }
-
-
- private static void ObservableGetValue()
- {
- IObservable<string> obj = Observable.Generate(
- 0,
- _ => true,
- i => i + 1,
- i => new string('#', i),
- i => TimeSelector(i));
-
-
- using (obj.Subscribe(Console.WriteLine))
- {
- Console.WriteLine("Press any key to exit!!!");
- Console.ReadLine();
- }
- }
-
-
- private static TimeSpan TimeSelector(int i)
- {
- return TimeSpan.FromSeconds(i);
- }
- }
- }
Hence, with the above change in place, when I run the app, it will give us the following output.
You can also download the source code from
here.
With this, I would like to wrap this session here. Thanks for joining me.