Code Tidy - Pastebin

New     Fork     Embed     View raw     Report

TextBox with hints - csharp

Embed

You can embed this paste into a blog or website with this code:

<iframe class="codetidy" type="text/html" width="100%" src="http://codetidy.com/paste/embed/1674" frameborder="0"></iframe>

Add comment

Captcha
  1. using System;?
  2. using System.Collections.Generic;?
  3. using System.Globalization;?
  4. using System.Linq;?
  5. using System.Reactive;?
  6. using System.Reactive.Concurrency;?
  7. using System.Reactive.Linq;?
  8. using System.Text;?
  9. using System.Windows;?
  10. using System.Windows.Controls;?
  11. using System.Windows.Data;?
  12. using System.Windows.Documents;?
  13. using System.Windows.Input;?
  14. using System.Windows.Media;?
  15. using System.Windows.Media.Imaging;?
  16. using System.Windows.Navigation;?
  17. using System.Windows.Shapes;?
  18.  ?
  19. namespace WpfApplication2?
  20. {?
  21.     /// <summary>?
  22.     /// Interaction logic for MainWindow.xaml?
  23.     /// </summary>?
  24.     public partial class MainWindow : Window?
  25.     {?
  26.         public MainWindow()?
  27.         {?
  28.             InitializeComponent();?
  29.             Observable.FromEventPattern(textBox, "TextChanged").Throttle(TimeSpan.FromMilliseconds(500)).ObserveOnDispatcher().Subscribe(OnTextChanged);?
  30.         }?
  31.  ?
  32.         public void OnTextChanged(EventPattern<EventArgs> eventPatternArgs)?
  33.         {?
  34.             var textBox = eventPatternArgs.Sender as TextBox;?
  35.             if(textBox != null)?
  36.             {?
  37.                var adornerLayer = AdornerLayer.GetAdornerLayer(textBox);?
  38.  ?
  39.                 textBoxHintAdorner = new TextBoxHintAdorner(textBox);?
  40.                 adornerLayer.Add(textBoxHintAdorner);?
  41.             }?
  42.  ?
  43.             disposable = Observable.FromEventPattern(textBox, "TextChanged").ObserveOnDispatcher().Subscribe(OnRemoveAdorner);?
  44.         }?
  45.  ?
  46.         public void OnRemoveAdorner(EventPattern<EventArgs> eventPatternArgs)?
  47.         {?
  48.             var textBox = eventPatternArgs.Sender as TextBox;?
  49.             if (textBox != null)?
  50.             {?
  51.                 var adornerLayer = AdornerLayer.GetAdornerLayer(textBox);?
  52.  ?
  53.                 if (textBoxHintAdorner != null)?
  54.                     adornerLayer.Remove(textBoxHintAdorner);?
  55.             }?
  56.  ?
  57.             disposable.Dispose();?
  58.         }?
  59.  ?
  60.         private IDisposable disposable;?
  61.         private TextBoxHintAdorner textBoxHintAdorner;?
  62.     }?
  63.  ?
  64.     public class TextBoxHintAdorner : Adorner?
  65.     {?
  66.         public TextBoxHintAdorner(UIElement adornedElement) : base(adornedElement)?
  67.         {?
  68.             ?
  69.         }?
  70.  ?
  71.         protected override void OnRender(DrawingContext drawingContext)?
  72.         {?
  73.             var textBox = AdornedElement as TextBox;?
  74.             if(textBox == null)?
  75.                 return;?
  76.             ?
  77.             String hintText = TextHint.GetHint(textBox.Text);?
  78.  ?
  79.             var formattedText = new FormattedText(hintText, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 10, Brushes.Black);?
  80.  ?
  81.             Rect rect = textBox.GetRectFromCharacterIndex(textBox.Text.Length);?
  82.  ?
  83.             drawingContext.DrawRectangle(Brushes.Yellow, null, new Rect(rect.TopRight.X + 10, rect.TopRight.Y, formattedText.Width, formattedText.Height));?
  84.             drawingContext.DrawText(formattedText, new Point(rect.BottomRight.X + 10, 0));?
  85.         }?
  86.     }?
  87.  ?
  88.     public class TextHint?
  89.     {?
  90.         public static String GetHint(String text)?
  91.         {?
  92.             int words = text.Split(new char[] {' '}).Count();?
  93.             if(words == 1)?
  94.                 return "circle|polygon";?
  95.             if (words == 2)?
  96.                 return "<n> sides";?
  97.             if (words == 4)?
  98.                 return "<x> colour";?
  99.  ?
  100.             return string.Empty;?
  101.         }?
  102.     }?
  103. }?
© 2011 Code Tidy