ak2291

Untitled

Aug 4th, 2019
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.Design.Serialization;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Forms.Integration;
  7. using System.Windows.Forms.Design;
  8. using System.Windows.Documents;
  9. using ToGetSpellCheck;
  10.  
  11.  
  12. [Designer(typeof(ControlDesigner))]
  13. class SpellCheck : ElementHost
  14. {
  15.     private TextBox box;
  16.     public SpellCheck()
  17.     {
  18.         box = new TextBox();
  19.         base.Child = box;
  20.         box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
  21.         box.SpellCheck.IsEnabled = true;
  22.         box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
  23.         this.Size = new System.Drawing.Size(100, 50);
  24.         box.PreviewMouseMove += OpenContextMenuOnMouseMove;
  25.     }
  26.  
  27.     private void OpenContextMenuOnMouseMove(object sender, System.Windows.Input.MouseEventArgs mouseEventArgs)
  28.     {
  29.  
  30.  
  31.         var textBox = sender as System.Windows.Controls.TextBox;
  32.  
  33.         var mouseOverTextIndex = textBox.GetCharacterIndexFromPoint(mouseEventArgs.GetPosition(textBox), false);
  34.         int spellingErrorIndex = textBox.GetNextSpellingErrorCharacterIndex(mouseOverTextIndex, LogicalDirection.Forward);
  35.  
  36.         // Pointer is not over text or no spelling errors
  37.         if (mouseOverTextIndex.Equals(-1) || spellingErrorIndex.Equals(-1))
  38.         {
  39.             return;
  40.         }
  41.  
  42.         int startOfWordIndex = mouseOverTextIndex;
  43.         while (startOfWordIndex != -1 && !textBox.Text[startOfWordIndex].Equals(' '))
  44.         {
  45.             startOfWordIndex--;
  46.         }
  47.  
  48.         var endOfWordIndex = textBox.Text.IndexOf(" ", mouseOverTextIndex, StringComparison.OrdinalIgnoreCase);
  49.  
  50.         if (endOfWordIndex.Equals(-1))
  51.         {
  52.             endOfWordIndex = textBox.Text.Length - 1;
  53.         }
  54.  
  55.         //  Spelling error doesn't belong to current mouse over word
  56.         if (spellingErrorIndex < startOfWordIndex || spellingErrorIndex > endOfWordIndex)
  57.         {
  58.             return;
  59.         }
  60.  
  61.         using (CustomPopup popup = new CustomPopup())
  62.         {
  63.  
  64.             foreach (var suggestion in textBox.GetSpellingError(spellingErrorIndex).Suggestions)
  65.             {
  66.                 var suggestionButton = new System.Windows.Forms.Button() { Text = suggestion };
  67.                 var eventArgs = new FixSpellingErrorEventArgs()
  68.                 {
  69.                     TextBox = textBox,
  70.                     WordStartIndex = startOfWordIndex,
  71.                     WordEndIndex = endOfWordIndex,
  72.                     Suggestion = suggestion
  73.                 };
  74.  
  75.                 suggestionButton.Click += (s, e) => FixSpellingError(eventArgs);
  76.  
  77.                 popup.FlowLayoutPanel1.Controls.Add(suggestionButton);
  78.             }
  79.  
  80.             popup.SetDesktopLocation((int)mouseEventArgs.GetPosition(textBox).X, (int)mouseEventArgs.GetPosition(textBox).Y);
  81.  
  82.             popup.ShowDialog(this);
  83.         }
  84.  
  85.     }
  86.  
  87.     private void FixSpellingError(FixSpellingErrorEventArgs e)
  88.     {
  89.         e.TextBox.SelectionStart = e.WordStartIndex;
  90.         e.TextBox.SelectionLength = e.WordEndIndex - e.WordStartIndex;
  91.         e.TextBox.SelectedText = e.Suggestion;
  92.     }
  93.  
  94.     class FixSpellingErrorEventArgs : EventArgs
  95.     {
  96.         public System.Windows.Controls.TextBox textBox { get; set; }
  97.         public int WordStartIndex { get; set; }
  98.         public int WordEndIndex { get; set; }
  99.         public string Suggestion { get; set; }
  100.  
  101.     }
  102.  
  103. }
Add Comment
Please, Sign In to add comment