【问题标题】:code doesn't always highlight the selected text in a richtextbox代码并不总是突出显示富文本框中的选定文本
【发布时间】:2019-10-03 03:16:55
【问题描述】:

richTextBox1 包含文本 我单击一个单词,控制台会为我显示该单词,并突出显示/选择我单击的单词。

为此,我保留了我单击的字符的索引,然后左右移动,直到点击空格、-1 或文件结尾。现在我有了单词开头和结尾的索引。最后两行应该选择这两个索引之间的单词。

但是,有时它会突出显示我想要的单词,有时它会突出显示我单击的字符右侧的所有单词。

“奥马尔你好,你要去哪里”
如果我点击 hello 中的 h,它会突出显示 hello where 而不是突出显示 hello
如果我单击 o in going,它将仅按应有的方式突出显示
如果我点击你里面的o,它会突出显示你去

我使用控制台检查了单词的开始和结束索引,由于某种原因它们总是正确的,除了我点击的单词之外,还选择了其他单词

private void richTextBox1_Click(object sender, EventArgs e)
{
    int length = richTextBox1.Text.Length;
    int rightPart = richTextBox1.SelectionStart;
    int leftPart = richTextBox1.SelectionStart - 1;
    string rightText = "";
    string leftText = "";

    while (rightPart != length)
    {
        if (richTextBox1.Text[rightPart].ToString().CompareTo(" ") != 0)
        {
            rightText += richTextBox1.Text[rightPart];
            rightPart++;
        }

        else
        {
            break;
        }
    }

    while (leftPart != -1)
    {
        if (richTextBox1.Text[leftPart].ToString().CompareTo(" ") != 0)
        {
            leftText = richTextBox1.Text[leftPart] + leftText;
            leftPart--;
        }

        else
        {
            break;
        }
    }

    leftPart++;
    Console.WriteLine("\nSelected word is " + leftText + rightText + "\n");

    richTextBox1.SelectionStart = leftPart;
    richTextBox1.SelectionLength = rightPart;       
}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    问题似乎是您将SelectionLength 设置为等于rightPart。请记住,此属性表示选择的长度,而不是选择的最后一个索引。

    请尝试更改代码以通过获取leftPartrightPart 之间的差异来计算长度:

    richTextBox1.SelectionLength = rightPart - leftPart;
    

    对于它的价值,你的代码可以缩短一点:

    private void richTextBox1_Click(object sender, EventArgs e)
    {
        if (richTextBox1.TextLength == 0) return;
        int rightPart = richTextBox1.SelectionStart;
        int leftPart = richTextBox1.SelectionStart - 1;
    
        while (rightPart < richTextBox1.TextLength && richTextBox1.Text[rightPart] != ' ') 
        {
            rightPart++;
        }
    
        while (leftPart > -1 && richTextBox1.Text[leftPart] != ' ') 
        {
            leftPart--;
        }
    
        leftPart++;
    
        Console.WriteLine($"\nSelected word is " + 
            richTextBox1.Text.Substring(leftPart, rightPart - leftPart) + "\n");
    
        richTextBox1.SelectionStart = leftPart;
        richTextBox1.SelectionLength = rightPart - leftPart;
    }
    

    甚至更多,使用IndexOfLastIndexOf 而不是循环来查找空格:

    private void richTextBox1_Click(object sender, EventArgs e)
    {
        if (richTextBox1.TextLength == 0) return;
    
        // Find the space before this word and after this word
        var selStart = Math.Min(richTextBox1.SelectionStart, richTextBox1.TextLength - 1);
        var firstSpace = richTextBox1.Text.LastIndexOf(' ', selStart);
        var lastSpace = richTextBox1.Text.IndexOf(' ', selStart);
    
        var start = firstSpace + 1;
        var length = (lastSpace < 0 ? richTextBox1.TextLength : lastSpace) - start;
    
        Console.WriteLine($"\nSelected word is {richTextBox1.Text.Substring(start, length)} \n");
    
        richTextBox1.SelectionStart = start;
        richTextBox1.SelectionLength = length;
    }
    

    【讨论】:

    • 一如既往地对错误的简单程度一记耳光。非常感谢您两次改进代码。现在我的下一个目标是在该行的不同位置选择一行或多个单词。再次感谢你。现在我可以做下一个任务了
    猜你喜欢
    • 2013-01-23
    • 2014-02-09
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2013-01-05
    • 2012-06-26
    • 2010-11-08
    • 2020-09-11
    相关资源
    最近更新 更多