2013-08-22 4 views
4

Связанные с этим д: Displaying tooltip on mouse hover of a textОтображение всплывающей в MouseHover Текст

Как я могу сделать то же самое, по ключевому слову в RichTextBox, но не ссылка.

образец:

в RichTextBox я типа:

Hello World, Как вы?

Я установил Навешивание слова «Мир» для всплывающей подсказки, и когда он наведет всплывающее подсказку, появится «Это мир»!

, но если он находится над RichTextBox или в слове «World», всплывающая подсказка исчезнет.

Надеюсь, вы могли бы мне помочь. Больше силы и GodBless!

+0

Понравилась PlaceHolder? –

+0

Я даже не знал, что @RameshRajendran что это? извините новичку здесь – Elegiac

+0

Посмотрите эти образцы: http://stackoverflow.com/questions/16008303/tooltip-in-text-inside-richtextbox http://stackoverflow.com/questions/16142890/tooltip-in-specifictext -inside-richtextbox-using-mouseover –

ответ

3

Это немного просто. Вы должны использовать метод вашего RichTextBox, чтобы получить Char index под номером Mouse Pointer, сделать несколько простых циклов, чтобы найти целое слово, а затем показать его в файле Tooltip popup. Вот код:

string punctuations = " ,.;!?'\")]}\n"; 
    //This saves your words with their corresponding definitions/details 
    Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase); 
    ToolTip tt = new ToolTip(); 
    int k; 
    int lineBreakIndex = 60; 
    int textHeight; 
    //MouseMove event handler for your richTextBox1 
    private void richTextBox1_MouseMove(object sender, MouseEventArgs e){ 
     if (richTextBox1.TextLength == 0) return; 
     Point lastCharPoint = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength - 1); 
     if (e.Y > textHeight || (e.Y >= lastCharPoint.Y && e.X > lastCharPoint.X + textHeight - lastCharPoint.Y))    
     { 
      tt.Hide(richTextBox1); 
      k = -1; 
      return; 
     } 
     int i = richTextBox1.GetCharIndexFromPosition(e.Location);    
     int m = i, n = i; 
     while (m>-1&&!punctuations.Contains(richTextBox1.Text[m])) m--;    
     m++; 
     while (n<richTextBox1.TextLength&&!punctuations.Contains(richTextBox1.Text[n])) n++; 
     if (n > m){ 
      string word = richTextBox1.Text.Substring(m, n - m); 
      if (dict.ContainsKey(word)){ 
       if (k != m){ 
        tt.ToolTipTitle = word; 
        tt.Show(dict[word], richTextBox1, e.X, e.Y + 10); 
        k = m; 
       } 
      } 
      else{ 
       tt.Hide(richTextBox1); 
       k = -1; 
      } 
     } 
    } 
    //This will get the entry text with lines broken. 
    private string GetEntryText(string key){ 
     string s = dict[key]; 
     int lastLineEnd = lineBreakIndex; 
     for (int i = lastLineEnd; i < s.Length; i += lineBreakIndex) 
     { 
      while (s[i] != ' '){ 
       if (--i < 0) break;      
      } 
      i++; 
      s = s.Insert(i, "\n"); 
      lastLineEnd = i+1; 
     } 
     return s; 
    } 
    //MouseLeave event handler for your richTextBox1 
    private void richTextBox1_MouseLeave(object sender, EventArgs e){ 
     tt.Hide(richTextBox1); 
     k = -1; 
    } 
    //ContentsResized event handler for your richTextBox1 
    private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e) 
    { 
     textHeight = e.NewRectangle.Height; 
    } 
    //Here are some sample words with definitions: 
    dict.Add("world", "- World is a common name for the whole of human civilization, specifically human experience, history, or the human condition in general, worldwide, i.e. anywhere on Earth."); 
    dict.Add("geek", "- A person who is single-minded or accomplished in scientific or technical pursuits but is felt to be socially inept"); 

enter image description here

+0

sir Я копирую этот скрипт, но не работает в моей dunno, в чем проблема – Elegiac

+0

@Elegiac Вы знаете, как зарегистрировать 'обработчики событий' в C#? –

+0

да, сэр, извините, что постараюсь это проверить – Elegiac

Смежные вопросы