2013-08-22 175 views
4

與此相關的問題: Displaying tooltip on mouse hover of a text顯示工具提示,以MouseHover文本

我怎樣才能做到在RichTextBox的一個關鍵字,但不是一個鏈接同樣的事情。

樣本:

RichTextBox中I型:

世界您好,你怎麼樣?

我設置懸停單詞「世界」的工具提示,當它徘徊工具提示將出現「這是世界」!

但是,如果RichTextBox或單詞「World」本身放出,工具提示將消失。

希望你能幫助我,提前感謝很多。更多的權力和GodBless!

+0

你喜歡佔位符? –

+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

這有點簡單。你必須用你的RichTextBoxGetCharIndexFromPosition方法得到Char indexMouse 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

先生我複製這些腳本,但不在我的工作不知道最新的概率 – Elegiac

+0

@Elegiac你知道如何註冊在C#中的事件處理程序? –

+0

是的,先生對不起生病嘗試檢查 – Elegiac