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);
}
}
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...
so I solved this problem by puting following code in Fram_main_load event procedure like this
As there is nothing in this question (but its age) that requires a solution in
Windows.Forms
, here is a way to do this inWPF
in code-behind.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
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
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
hope it helps
peace
Use:
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.