Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Getting the word at the current mouse position

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:C#
Version:1.0
Released:March 26, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Studio 2005
Download code:tx_chars_sample.zip Download [13.5 KB, ZIP]
Click to enlarge

Sometimes, it is necessary to know the word at the current mouse position, in order to display additional information to the end-user.

This sample displays a tool tip with the word's text when the end-user moves the mouse over a word.

The function GetWordFromIndex must be called with the character index and returns the complete word at this position. To avoid a potential flicker that is caused by changing the input position, this sample does not use the Find method to search for the word delimiters like a comma, period or any other punctuation marks.

Instead of the Find method, the function uses the internal .NET String methods IndexOfAny and LastIndexOfAny. These methods return an index of a specific character. The SubString method is used to build the detected word from the complete document's text.

  1. private string GetWordFromIndex(int Index)
  2. {
  3. string CompleteText = textControl1.Text.Replace("\r\n","\n");
  4. Char[] chars = new Char[] { ',', ';', ':', '!', '?', '.', ' ', (char)9, (char)10 };
  5.  
  6. int lastChar = CompleteText.IndexOfAny(chars, Index);
  7.  
  8. if(lastChar != -1)
  9. CompleteText = CompleteText.Substring(0, lastChar);
  10.  
  11. int firstChar = CompleteText.LastIndexOfAny(chars);
  12.  
  13. return CompleteText.Substring(firstChar + 1);
  14. }

The above function is called in TX Text Control's MouseMove event. The GetItem method of the TextChars collection of TX Text Control returns the character index of a specific location. This method is called with the return value of the MouseMove event. The return value of the GetWordFromIndex function is used to display a tool tip above the specific word.

This approach has the advantage that the current input position must not be changed.

  1. private void textControl1_MouseMove(object sender, MouseEventArgs e)
  2. {
  3. int charIndex = textControl1.TextChars.GetItem(e.Location, true).Number;
  4. string currentWord = GetWordFromIndex(charIndex);
  5.  
  6. if (currentWord != "")
  7. {
  8. toolTip1.ToolTipTitle = currentWord;
  9. toolTip1.Show("Index: " + charIndex.ToString(), this, e.X + 20, e.Y - 10);
  10. }
  11. else
  12. toolTip1.Hide(this);
  13. }

The sample requires Visual Studio 2005 and at least a trial version of TX Text Control .NET 13.0.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place