【问题标题】:Displaying ToolTip to MouseHover Text向 MouseHover 文本显示工具提示
【发布时间】:2013-08-22 03:29:40
【问题描述】:

与这个问题有关: Displaying tooltip on mouse hover of a text

我怎样才能对 RichTextBox 中的关键字而不是链接做同样的事情?

在 RichTextBox 中输入:

Hello World, How are You?

我为工具提示设置了悬停“世界”一词,当它悬停时,工具提示会显示“这是世界”!

但如果它悬停在 RichTextBox 之外或“世界”一词本身,工具提示就会消失。

【问题讨论】:

  • 你喜欢 PlaceHolder 吗?
  • 我什至不知道@RameshRajendran 那是什么?对不起这里的新手
  • @RameshRajendran 这是我很久以前的帖子,但它不能正常工作,关于悬停/显示和处置工具提示的问题
  • @Elegiac 只显示悬停单词甚至短语(包含一些单词)的工具提示?

标签: c# winforms tooltip mousehover


【解决方案1】:

这有点简单。您必须使用您的RichTextBoxGetCharIndexFromPosition 方法来获取Mouse Pointer 下的Char index,进行一些简单的循环以找到整个单词,然后正常显示在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");

【讨论】:

  • 先生,我复制了那些脚本,但在我的不工作不知道问题是什么
  • @Elegiac 你知道如何在c#中注册event handlers吗?
  • 太棒了,先生!先生,它奏效了。 ..但仅在第一个词世界中。在别人怎么样?和极客一样
  • @Elegiac 你的 RichTextBox 应该包含这样的词,然后你只需将鼠标悬停在关键词上。这些关键字应该添加到我的代码中的Dictionary dict 中(连同它们的含义/细节/定义)。
  • 我不知道为什么,但是当我输入带有 geeks/world 的随机单词时,有些还可以,但有些地方不工作 geeks 和 world
猜你喜欢
  • 2012-07-17
  • 1970-01-01
  • 2011-09-14
  • 2014-02-07
  • 2018-10-24
  • 1970-01-01
  • 2011-07-20
  • 2013-09-02
  • 1970-01-01
相关资源
最近更新 更多