Displaying tooltip on mouse hover of a text

164,300

Solution 1

Just add ToolTip tool from toolbox to the form and add this code in a mousemove event of any control you want to make the tooltip start on its mousemove

private void textBox3_MouseMove(object sender, MouseEventArgs e)
    {
      toolTip1.SetToolTip(textBox3,"Tooltip text"); // you can change the first parameter (textbox3) on any control you wanna focus
    }

hope it helps

peace

Solution 2

Well, take a look, this works, If you have problems please tell me:

using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        ToolTip tip = new ToolTip();
        void richTextBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!timer1.Enabled)
            {
                string link = GetWord(richTextBox1.Text, richTextBox1.GetCharIndexFromPosition(e.Location));
                //Checks whether the current word i a URL, change the regex to whatever you want, I found it on www.regexlib.com.
//you could also check if current word is bold, underlined etc. but I didn't dig into it.
                if (System.Text.RegularExpressions.Regex.IsMatch(link, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$"))
                {
                    tip.ToolTipTitle = link;
                    Point p = richTextBox1.Location;
                    tip.Show(link, this, p.X + e.X,
                        p.Y + e.Y + 32, //You can change it (the 35) to the tooltip's height - controls the tooltips position.
                        1000);
                    timer1.Enabled = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e) //The timer is to control the tooltip, it shouldn't redraw on each mouse move.
        {
            timer1.Enabled = false;
        }

        public static string GetWord(string input, int position) //Extracts the whole word the mouse is currently focused on.
        {
            char s = input[position];
            int sp1 = 0, sp2 = input.Length;
            for (int i = position; i > 0; i--)
            {
                char ch = input[i];
                if (ch == ' ' || ch == '\n')
                {
                    sp1 = i;
                    break;
                }
            }

            for (int i = position; i < input.Length; i++)
            {
                char ch = input[i];
                if (ch == ' ' || ch == '\n')
                {
                    sp2 = i;
                    break;
                }
            }

            return input.Substring(sp1, sp2 - sp1).Replace("\n", "");
        }
    }
}

Solution 3

You shouldn't use the control private tooltip, but the form one. This example works well:

public partial class Form1 : Form
{
    private System.Windows.Forms.ToolTip toolTip1;

    public Form1()
    {
        InitializeComponent();
        this.components = new System.ComponentModel.Container();
        this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);

        MyRitchTextBox myRTB = new MyRitchTextBox();
        this.Controls.Add(myRTB);

        myRTB.Location = new Point(10, 10);
        myRTB.MouseEnter += new EventHandler(myRTB_MouseEnter);
        myRTB.MouseLeave += new EventHandler(myRTB_MouseLeave);
    }


    void myRTB_MouseEnter(object sender, EventArgs e)
    {
        MyRitchTextBox rtb = (sender as MyRitchTextBox);
        if (rtb != null)
        {
            this.toolTip1.Show("Hello!!!", rtb);
        }
    }

    void myRTB_MouseLeave(object sender, EventArgs e)
    {
        MyRitchTextBox rtb = (sender as MyRitchTextBox);
        if (rtb != null)
        {
            this.toolTip1.Hide(rtb);
        }
    }


    public class MyRitchTextBox : RichTextBox
    {
    }

}

Solution 4

This is not elegant, but you might be able to use the RichTextBox.GetCharIndexFromPosition method to return to you the index of the character that the mouse is currently over, and then use that index to figure out if it's over a link, hotspot, or any other special area. If it is, show your tooltip (and you'd probably want to pass the mouse coordinates into the tooltip's Show method, instead of just passing in the textbox, so that the tooltip can be positioned next to the link).

Example here: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.getcharindexfromposition(VS.80).aspx

Share:
164,300
Sujay Ghosh
Author by

Sujay Ghosh

Updated on July 09, 2022

Comments

  • Sujay Ghosh
    Sujay Ghosh almost 2 years

    I want to display a tooltip when the mouse hovers over a link in my custom rich edit control. Consider the following text:

    We all sleep at night .

    In my case the word sleep is a link.

    When the user moves the mouse under the link, in this case "sleep", I want to display a tooltip for the link.

    The following came to my mind, but they are not working

    1) Trapping OnMouseHover

    if(this.Cursor == Cursors.Hand)
       tooltip.Show(textbox,"My tooltip");
    else
       tooltip.Hide(textbox);
    

    But this does not work out.

    UPDATE

    The links mentioned are not URLs, i.e these are custom links, so Regex won't be of much help here, it can be any text. The user can choose to create it a a link.

    Though I have not tried GetPosition method, I dont think it would be that elegant in terms of design and maintenance.

    Let me say I have the following line, in my richedit box

    We sleep at night. But the bats stay awake. Cockroaches become active at night.

    In the above sentence, I want three different tooltips, when the mouse hovers over them.

    sleep -> Human beings
    awake -> Nightwatchman here
    active -> My day begins
    

    I trapped OnMouseMove as follows:

    Working- with Messagebox

    OnMouseMove( )
    {
    
       // check to see if the cursor is over a link
       // though this is not the correct approach, I am worried why does not a tooltip show up
       if(this.Cursor.current == Cursors.hand )
       {
         Messagebox.show("you are under a link");
       }
    }
    

    Not Working - with Tooltip - Tooltip does not show up

    OnMouseMove( MouseventArgs e )
    {
    
       if(cursor.current == cursors.hand )
       {
         tooltip.show(richeditbox,e.x,e.y,1000);
       }
    }
    
  • Sujay Ghosh
    Sujay Ghosh almost 15 years
    I did the following; just for testing in the MouseMove event of the richedit box. if(Cursor.Current == Cursors.Hand) Messagebox.Show("My Tooltip); But my tooltip is not getting displayed, once I replace the Messagebox with the tooltip.show() as below if(Cursor.Current == Cursors.Hand) this.ttpLink.Show("Hover",txtBox,e.X,e.Y,1000); Am I missing something ?
  • Sujay Ghosh
    Sujay Ghosh almost 15 years
    Jean, even I get the text how do I know the atring ia link,
  • anion
    anion over 6 years
    it is enough to set set toolTip1.SetToolTip(textBox3,"Tooltip text"); only once, not every time the mousemove-event is raised.