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
I would also like to add something here that if you load desired form that contain tooltip controll before the program's run then tool tip control on that form will not work as described below...
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
objfrmmain = new Frm_Main();
Showtop();//this is procedure in program.cs to load an other form, so if that contain's tool tip control then it will not work
Application.Run(objfrmmain);
}
so I solved this problem by puting following code in Fram_main_load event procedure like this
private void Frm_Main_Load(object sender, EventArgs e)
{
Program.Showtop();
}
For the sake of ease of use and understandability.
You can simply put a Tooltip anywhere on your form (from toolbox). You will then be given an options in the Properties of everything else in your form to determine what is displayed in that Tooltip (it reads something like "ToolTip on toolTip1"). Anytime you hover on an object, the text in that property will be displayed as a tooltip.
This does not cover custom on-the-fly tooltips like the original question is asking for. But I am leaving this here for others that do not need
Use:
ToolTip tip = new ToolTip();
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
Cursor a = System.Windows.Forms.Cursor.Current;
if (a == Cursors.Hand)
{
Point p = richTextBox1.Location;
tip.Show(
GetWord(richTextBox1.Text,
richTextBox1.GetCharIndexFromPosition(e.Location)),
this,
p.X + e.X,
p.Y + e.Y + 32,
1000);
}
}
Use the GetWord function from my other answer to get the hovered word. Use timer logic to disable reshow the tooltip as in prev. example.
In this example right above, the tool tip shows the hovered word by checking the mouse pointer.
If this answer is still not what you are looking fo, please specify the condition that characterizes the word you want to use tooltip on. If you want it for bolded word, please tell me.
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", "");
}
}
}
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
If you are using RichTextBox control. You can simply define the ToolTip object and show the tool-tip as the text is selected by moving the mouse inside the RichTextBox control.
ToolTip m_ttInput = new ToolTip(); // define as member variable
private void rtbInput_SelectionChanged(object sender, EventArgs e)
{
if (rtbInput.SelectedText.Length > 0)
{
m_ttInput.Show(rtbInput.SelectedText.Length.ToString(), rtbInput, 1000);
}
}