首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ToolTip显示为MouseHover文本

将ToolTip显示为MouseHover文本
EN

Stack Overflow用户
提问于 2013-08-22 03:29:40
回答 1查看 2.4K关注 0票数 5

与此相关的问题:Displaying tooltip on mouse hover of a text

如何对RichTextBox中的关键字而不是链接执行相同的操作?

在RichTextBox I中,键入:

代码语言:javascript
复制
Hello World, How are You?

我为工具提示设置了"World“这个词,当它悬停时,tooltip会出现,说”这就是世界“!

但是如果它不存在于RichTextBox或"World“这个词中,工具提示就会消失。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-22 07:38:02

有点简单。您必须使用RichTextBoxRichTextBox方法来获取Mouse Pointer下面的Char index,做一些简单的循环来查找整个单词,然后通常在Tooltip popup中显示它。以下是代码:

代码语言:javascript
复制
    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");

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18371209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档