How to do it...

  1. Start by adding a new Windows forms project to your solution.
  1. Call the project winformRx and click on the OK button:
  1. In Toolbox, search for the TextBox control and add it to your form.
  1. Finally, add a label control to your form:
  1. Right-click on your winformRx project and select Manage NuGet Packages... from the context menu.
  1. In the search textbox, enter System.Reactive to search for the NuGet package and click on the Install button.
  1. Visual Studio will ask you to review the changes it's about to make to your project. Click on the OK button.
  1. Before the installation starts, you might need to accept the license agreement by clicking on the I Accept button.
  2. After the installation completes, you should see the newly added references to your winformRx project if you expand the References for the project:
  1. Finally, right-click on the project and set winformRx as your startup project by clicking on the Set as StartUp Project from the context menu.
  1. Create the form load event handler for the form by double-clicking anywhere on the Windows form. To this form, add the Observable keyword. You will notice that the keyword is immediately underlined. This is because you are missing the reference to the LINQ assembly of System.Reactive.
  1. To add this, press Ctrl + . (period) to bring up the possible suggestions to fix the issue. Select to add the using System.Reactive.Linq namespace to your project.
  1. Continue adding the following code to your form load event. Basically, you are using LINQ and telling the compiler that you want to select the text from the event pattern that matches the text changed event of the textbox on the form called textBox1. After you have done that, add a subscription to the variable and tell it to output whatever it finds in the text to the label on the form called label1:
        private void Form1_Load(object sender, EventArgs e) 
{
var searchTerm = Observable.FromEventPattern<EventArgs>(
textBox1, "TextChanged").Select(x => ((TextBox)x.Sender).Text);

searchTerm.Subscribe(trm => label1.Text = trm);
}
When we added the textbox and label to our form, we left the control names as default. If, however, you changed the default names, you would need to specify those names instead of textBox1 and label1 for the controls on the form.
  1. Click on the run button to run your application. The Windows form will be displayed with the textbox and label on it.
  1. Notice that as you type, the text is output to the label on the form:
  1. Let's jazz things up a bit by adding in a Where condition to the LINQ statement. We will specify that the text string must only select the text when it ends with a period. This means that the text will only be displayed in the label after each full sentence. As you can see, we aren't doing anything special here. We are merely using standard LINQ to query our datastream and return the results to our searchTerm variable:
        private void Form1_Load(object sender, EventArgs e) 
{
var searchTerm = Observable.FromEventPattern<EventArgs>(
textBox1, "TextChanged").Select(x => ((TextBox)x.Sender)
.Text).Where(text => text.EndsWith("."));

searchTerm.Subscribe(trm => label1.Text = trm);
}
  1. Run your application and start typing in a line of text. You will see that nothing is output to the label control as you type, as was evident in the previous example before we added in our Where condition:
  1. Add a period and start adding a second line of text:
  1. You will see that only after each period, the text entered is added to the label. Our Where condition is, therefore, working perfectly:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.76.72