我有一个多行文本框,根据给定的数据显示一些值(通常每行一个值)。
(为了弹出带有一些“替代”数据的工具提示)我想获取鼠标悬停在其上的单词(或至少是行),以便我可以找到要显示的替代项。
我对如何根据文本框和字体大小进行计算有一些想法,但我不知道该怎么走这条路,因为大小和字体可能会经常变化。
那么...有什么方法可以使用鼠标位置来抓取特定的文本框文本吗?
这是一个替代解决方案。将此 MouseMove 事件添加到您的 TextBox:
private void txtHoverWord_MouseMove(object sender, MouseEventArgs e)
{
if (!(sender is TextBox)) return;
var targetTextBox = sender as TextBox;
if(targetTextBox.TextLength < 1) return;
var currentTextIndex = targetTextBox.GetCharIndexFromPosition(e.Location);
var wordRegex = new Regex(@"(\w+)");
var words = wordRegex.Matches(targetTextBox.Text);
if(words.Count < 1) return;
var currentWord = string.Empty;
for (var i = words.Count - 1; i >= 0; i--)
{
if (words[i].Index <= currentTextIndex)
{
currentWord = words[i].Value;
break;
}
}
if(currentWord == string.Empty) return;
toolTip.SetToolTip(targetTextBox, currentWord);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)