Displaying tooltip on mouse hover of a text

前端 未结 9 1012
盖世英雄少女心
盖世英雄少女心 2020-11-29 07:22

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

9条回答
  •  抹茶落季
    2020-11-29 08:24

    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
        {
        }
    
    }
    

提交回复
热议问题