c# - 有没有办法将工具提示放在特定文本上?
问题描述
我正在尝试使用 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 设置了一个工具提示,但我希望它只是特定的文本。
解决方案
正如 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);
}
}
推荐阅读
- python - 是否可以使用python“流式传输”终端应用程序?
- google-bigquery - 如何以类似通配符的方式在 BigQuery 中查询多个 STRUCT
- batch-file - 如果文件被编辑,我如何在 bat 文件中调用 resgen?
- vue.js - 在不更改路由的情况下重新评估 Nuxt.js 中间件
- c++ - Qt 半透明 QOpenGLWidget 与背景奇怪地混合
- reactjs - 找不到模块:无法解析 node_modules\@emotion\styled\base\dist 中的“@emotion/react”
- html - 如何使我的文本更接近我的标题?
- excel - 使用通配符声明 const 变量
- java - 删除了 TestNG @BeforeMethod onlyForGroups 选项
- c# - 字典不包含 Dictionary.Keys.First() 的键