把单词放在光标下,我错在哪里
本文关键字:我错在哪里 光标 单词放 | 更新日期: 2023-09-27 18:05:50
我错在哪里?我想在网站上做一个单词到textbox2。很抱歉,因为我的工程学不好。:)
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 = textBox2.GetCharIndexFromPosition(e.Location);
var wordRegex = new Regex(@"('w+)");
var words = wordRegex.Matches(textBox2.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;
toolTip1.SetToolTip(textBox2, currentWord);
}
我相信您可能在代码中不小心指定了textBox2
而不是targetTextBox
。
试着修改如下:
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;
tooltip1.SetToolTip(targetTextBox, currentWord);
}
请注意,我将textBox2
更改为targetTextBox
,无论它出现在您的代码中。