简体   繁体   English

代码并不总是突出显示richtextbox中的选定文本

[英]code doesn't always highlight the selected text in a richtextbox

richTextBox1 contains text I click on a word and Console displays that word for me and it highlights/selects the word i clicked on. richTextBox1包含我单击一个单词的文本,控制台为我显示该单词,它突出显示/选择我单击的单词。

To do this, I keep the index of the character I clicked on then go left and right until I hit a space, -1, or end of file. 为此,我保持我单击的字符的索引然后左右移动,直到我命中空格,-1或文件结尾。 Now I have the indexes of the beginning and end of the word. 现在我有了单词开头和结尾的索引。 The last two lines are supposed to select the word between those two indexes. 最后两行应该选择这两个索引之间的单词。

However, what happens is that sometimes it highlights the word I want and sometimes it highlights all the words to the right of the character I clicked on. 然而,发生的事情是,有时它会突出显示我想要的单词,有时它会突出显示我单击的角色右侧的所有单词。

"omar hello where are you going" “奥马尔你好,你要去哪里”
If I click on h in hello, it highlights hello where instead of highlighting hello 如果我在hello中单击h,它会突出显示hello而不是突出显示hello
If I click on o in going, it will highlight going only as it should 如果我在前进中点击o,它将突出显示正常情况
If I click on o in you, it will highlight you going 如果我点击你的o,它会突出你的意思

I used console to check the start and end indexes of the word and they're always right yet for some reason, other words are selected in addition to the word i clicked on 我用console来检查单词的开始和结束索引,但由于某种原因它们总是正确的,除了单击的单词之外还选择了其他单词

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;       
}

The problem appears to be that you are setting the SelectionLength equal to rightPart . 问题似乎是您将SelectionLength设置为等于rightPart Remember this property represents the length of the selection, not the last index of the selection. 请记住,此属性表示选择的长度 ,而不是选择的最后一个索引。

Instead, try changing your code to calculate the length by getting the difference between leftPart and rightPart : 相反,尝试更改代码以通过获取leftPartrightPart之间的差异来计算长度:

richTextBox1.SelectionLength = rightPart - leftPart;

For what it's worth, your code can be shortened a little: 为了它的价值,您的代码可以缩短一点:

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;
}

Or even a little more, using IndexOf and LastIndexOf instead of loops to find the spaces: 甚至更多,使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM