Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#如何在richtextbox中只更新一行文本?_C#_Richtextbox - Fatal编程技术网

c#如何在richtextbox中只更新一行文本?

c#如何在richtextbox中只更新一行文本?,c#,richtextbox,C#,Richtextbox,如何仅更新richtextbox中的一行文本 String[] lines = richTextBox8.Lines; lines[2] += " "; richTextBox8.Lines = lines; 我使用这个代码部分来更新richtextbox的第二行,但是它扫描了我所有的richtextbox行,这需要很多次 所以我想更新一行的行文本 我该怎么做呢?请注意,您决不能直接触摸文本或行,否则之前的所有格式都会混乱 下面是一个函数,它可以在不破坏格式的情况下解决问题: void cha

如何仅更新richtextbox中的一行文本

String[] lines = richTextBox8.Lines;
lines[2] += " ";
richTextBox8.Lines = lines;
我使用这个代码部分来更新richtextbox的第二行,但是它扫描了我所有的richtextbox行,这需要很多次

所以我想更新一行的行文本


我该怎么做呢?

请注意,您决不能直接触摸
文本或
行,否则之前的所有格式都会混乱

下面是一个函数,它可以在不破坏格式的情况下解决问题:

void changeLine(RichTextBox RTB, int line, string text)
{
    int s1 = RTB.GetFirstCharIndexFromLine(line);
    int s2 = line < RTB.Lines.Count() - 1 ?
              RTB.GetFirstCharIndexFromLine(line+1) - 1 : 
              RTB.Text.Length;        
    RTB.Select(s1, s2 - s1);
    RTB.SelectedText = text;
}

谢谢,我正在使用modfy这一行,但我对richtextbox的最后一行有疑问:/是的,我错了。对不起,我已经更新了答案。现在对你有用吗?
changeLine(yourrichTextBox, 1, yourrichTextBox.Lines[1] + " ");