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
As there is nothing in this question (but its age) that requires a solution in Windows.Forms
, here is a way to do this in WPF
in code-behind.
TextBlock tb = new TextBlock();
tb.Inlines.Add(new Run("Background indicates packet repeat status:"));
tb.Inlines.Add(new LineBreak());
tb.Inlines.Add(new LineBreak());
Run r = new Run("White");
r.Background = Brushes.White;
r.ToolTip = "This word has a White background";
tb.Inlines.Add(r);
tb.Inlines.Add(new Run("\t= Identical Packet received at this time."));
tb.Inlines.Add(new LineBreak());
r = new Run("SkyBlue");
r.ToolTip = "This word has a SkyBlue background";
r.Background = new SolidColorBrush(Colors.SkyBlue);
tb.Inlines.Add(r);
tb.Inlines.Add(new Run("\t= Original Packet received at this time."));
myControl.Content = tb;
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
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
{
}
}