在鼠标悬停文本时显示工具提示

Suj*_*osh 19 .net c# tooltip richtextbox winforms

当鼠标悬停在我的自定义富编辑控件中的链接上时,我想显示工具提示.请考虑以下文本:

我们都晚上睡觉.

就我而言,睡眠这个词就是一个链接.

当用户在链接下移动鼠标时,在这种情况下"睡眠",我想显示链接的工具提示.

以下是我的想法,但他们没有工作

1)捕获OnMouseHover

if(this.Cursor == Cursors.Hand)
   tooltip.Show(textbox,"My tooltip");
else
   tooltip.Hide(textbox);
Run Code Online (Sandbox Code Playgroud)

但这没有用.

UPDATE

提到的链接不是 URL,即这些是自定义链接,因此Regex在这里不会有太多帮助,它可以是任何文本.用户可以选择创建链接.

虽然我没有尝试过GetPosition方法,但我认为在设计和维护方面它不会那么优雅.

让我说我在我的richedit框中有以下行

我们晚上睡觉.但蝙蝠保持清醒.蟑螂在夜间变得活跃.

在上面的句子中,当鼠标悬停在它们上面时,我想要三个不同的工具提示.

sleep -> Human beings
awake -> Nightwatchman here
active -> My day begins
Run Code Online (Sandbox Code Playgroud)

我陷入困境OnMouseMove如下:

使用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");
   }
}
Run Code Online (Sandbox Code Playgroud)

不工作 - 使用工具提示 - 工具提示不会显示

OnMouseMove( MouseventArgs e )
{

   if(cursor.current == cursors.hand )
   {
     tooltip.show(richeditbox,e.x,e.y,1000);
   }
}
Run Code Online (Sandbox Code Playgroud)

Shi*_*mmy 14

好吧,看看,这个有效,如果你有问题请告诉我:

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", "");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Maw*_*rdy 10

只需将工具箱中的工具提示工具添加到表单中,并将此代码添加到任何控件的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
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你

和平

  • 设置 set toolTip1.SetToolTip(textBox3,"Tooltip text"); 就足够了 仅一次,而不是每次引发 mousemove-event 时。 (2认同)

ser*_*hio 5

您不应该使用控件私有工具提示,而是使用表单.这个例子效果很好:

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

}
Run Code Online (Sandbox Code Playgroud)


jea*_*ean 2

这并不优雅,但您可以使用 RichTextBox.GetCharIndexFromPosition 方法返回鼠标当前位于的字符的索引,然后使用该索引来确定它是否位于链接、热点或任何其他特殊区域。如果是,请显示您的工具提示(您可能希望将鼠标坐标传递到工具提示的 Show 方法中,而不是仅传递文本框,以便工具提示可以位于链接旁边)。

示例如下: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.getcharindexfromposition (VS.80).aspx