首页 > 解决方案 > 有没有办法将工具提示放在特定文本上?

问题描述

我正在尝试使用 C# 在特定文本上放置工具提示。我一直无法做到这一点。我的以下代码如下:

火柴:

    string errors = @"SendLuaScript|SendCSharpScript|SendRubyScript";
                MatchCollection errorsMatches = Regex.Matches(richTextBox1.Text, errors);

为这些匹配设置属性:

foreach (Match m in errorsMatches)
            {
                ToolTip tip = new ToolTip();
                richTextBox1.SelectionStart = m.Index;
                richTextBox1.SelectionLength = m.Length;
                richTextBox1.SelectionColor = Color.DarkGray;
                tip.IsBalloon = true;
                tip.ToolTipIcon = ToolTipIcon.Error;
                tip.SetToolTip(richTextBox1, "ERROR. You must use void SendLuaScript.");

            }

但是,我知道这为整个 RichTextBox 设置了一个工具提示,但我希望它只是特定的文本。

标签: c#.netvisual-studio

解决方案


正如 TaW 所建议的,这里有一些粗略的启动代码,用于在富文本框中显示所需文本时显示工具提示:

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    bool toolTip = false;
    Point pt = new Point(e.X, e.Y);
    int index = richTextBox1.GetCharIndexFromPosition(pt);
    string errors = @"SendLuaScript|SendCSharpScript|SendRubyScript";
    MatchCollection errorsMatches = Regex.Matches(richTextBox1.Text, errors);
    foreach (Match m in errorsMatches)
    {
        if (index >= m.Index && index < m.Index + m.Length)
        {
            toolTip1.Show(m.Value, richTextBox1, pt);
            toolTip = true;
            break;
        }                
    }
    if (!toolTip)
    {
        toolTip1.Hide(richTextBox1);
    }
}

推荐阅读