C# 把单词放在光标下我错了什么?

C# 把单词放在光标下我错了什么?,c#,textbox,mouseevent,mouseover,getchar,C#,Textbox,Mouseevent,Mouseover,Getchar,我犯了什么错?我想在网站上留言到textbox2。很抱歉,我的英文不好:) private void txtoverword\u MouseMove(对象发送方,MouseEventArgs e){ 如果(!(发送方为文本框))返回; var targetTextBox=发送方作为文本框; if(targetTextBox.TextLength

我犯了什么错?我想在网站上留言到textbox2。很抱歉,我的英文不好:)

private void txtoverword\u MouseMove(对象发送方,MouseEventArgs e){
如果(!(发送方为文本框))返回;
var targetTextBox=发送方作为文本框;
if(targetTextBox.TextLength<1)返回;
var currentTextIndex=textBox2.GetCharIndexFromPosition(e.Location);
var wordRegex=newregex(@“(\w+));
var words=wordRegex.Matches(textBox2.Text);
如果(words.Count<1)返回;
var currentWord=string.Empty;
对于(var i=words.Count-1;i>=0;i--)
{

如果(单词[i].Index我相信您可能无意中在代码中指定了
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);
}
private void txtoverword\u MouseMove(对象发送方,MouseEventArgs e)
{
如果(!(发送方为文本框))返回;
var targetTextBox=发送方作为文本框;
if(targetTextBox.TextLength<1)返回;

var currentTextIndex=targetTextBox.GetCharIndexFromPosition(e.Location); var wordRegex=newregex(@“(\w+)); var words=wordRegex.Matches(targetTextBox.Text); 如果(words.Count<1)返回; var currentWord=string.Empty; 对于(var i=words.Count-1;i>=0;i--) {
if(words[i].Index您是否有任何错误、异常?我的错误是什么?这不是询问..targetTextBox.GetCharIndexFromPosition(e.Location)的好方法;不允许visual studio targettextbox没有getchar fonc。@kdr_81我想你是说
targettextbox
不支持
GetCharIndexFromPosition
方法。但是,从.Net 2.0开始,
TextBox
类上就存在此函数。你能试着澄清一下你遇到的问题吗g?现在支持:)但不工作我有两个问题tooltip1.SetToolTip(targetTextBox,currentWord);它的tooltip1或tooltip?以及我如何称呼这个方法:)@kdr_81它将是您的
工具提示
对象在代码中的名称。您最初在代码中将其命名为
toolTip1
。您需要创建一个
工具提示
对象,然后调用
SetToolTip
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);
}