Guest User

Time flies - rx C# example

a guest
Feb 17th, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Reactive.Linq;
  4. using System.Drawing;
  5. using System.Threading;
  6.  
  7. namespace rxforms
  8. {
  9.     public class MainForm: Form
  10.     {
  11.         public MainForm ()
  12.         {
  13.             InitializeComponent ();
  14.         }
  15.  
  16.         private void Reactive (string msg)
  17.         {
  18.             var mousemove = Observable.FromEventPattern<MouseEventArgs> (this, "MouseMove");
  19.             var message = msg.ToCharArray ();
  20.  
  21.             for (int i = 0; i < message.Length; i++) {
  22.                 var l = new Label () {
  23.                     Text = message [i].ToString (),
  24.                     AutoSize = true,
  25.                     TextAlign = ContentAlignment.MiddleCenter
  26.                 };
  27.  
  28.                 int closure = i;
  29.                 mousemove
  30.                     .Delay (TimeSpan.FromSeconds (0.07 * i)).ObserveOn(SynchronizationContext.Current)
  31.                     .Subscribe(e => {
  32.                         var left=l.Left = e.EventArgs.X + closure * 12 - 5;
  33.                         var top = e.EventArgs.Y + 15;
  34.                         l.Left = left;
  35.                         l.Top = top;
  36.                 });
  37.                 Controls.Add (l);
  38.             }
  39.         }
  40.  
  41.  
  42.         private System.ComponentModel.IContainer components = null;
  43.  
  44.         /// <summary>
  45.         /// Clean up any resources being used.
  46.         /// </summary>
  47.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  48.         protected override void Dispose (bool disposing)
  49.         {
  50.             if (disposing && (components != null)) {
  51.                 components.Dispose ();
  52.             }
  53.             base.Dispose (disposing);
  54.         }
  55.  
  56.    
  57.         /// <summary>
  58.         /// Required method for Designer support - do not modify
  59.         /// the contents of this method with the code editor.
  60.         /// </summary>
  61.         private void InitializeComponent ()
  62.         {
  63.             this.SuspendLayout();
  64.             //
  65.             // MainForm
  66.             //
  67.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  68.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  69.             this.ClientSize = new System.Drawing.Size(646, 430);
  70.             this.Name = "MainForm";
  71.             this.Text = "RX test";
  72.             this.Load += new System.EventHandler(this.MainForm_Load);
  73.             this.ResumeLayout(false);
  74.  
  75.         }
  76.  
  77.         private void MainForm_Load(object sender, EventArgs e)
  78.         {
  79.             Reactive("Time flies like an arrow.");
  80.         }
  81.     }
  82.  
  83.     class MainClass
  84.     {
  85.         [STAThread]
  86.         public static void Main(string[] args)
  87.         {
  88.             MainForm form = new MainForm();
  89.             Application.Run(form);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment