简体   繁体   English

RichTextBox的特定行的值不变

[英]Value of a specific line of RichTextBox not changing

I am trying to do very simple thing which should work but unfortunately its not working. 我正在尝试做一个很简单的事情,它应该可以工作,但是不幸的是它不起作用。

I have a RichTextBox component on my Winform . 我有一个RichTextBox对我的组件Winform I am trying to change text value of some lines of the RichTextBox but its not changing the value. 我正在尝试更改RichTextBox的某些行的文本值,但不更改其值。 Here is my code: 这是我的代码:

    private void button1_Click(object sender, EventArgs e)
    {
        for(int i=0; i < richTextBox1.Lines.Length; i++)
        {
            if(richTextBox1.Lines[i] == "ok")
            {
                richTextBox1.Lines[i] = "Done";
            }
        }
    }

I put break point and I notice that it executes 我放置了断点,我注意到它执行了

richTextBox1.Lines[i] = "Done";

But it does not change the value at all. 但这根本不会改变值。 Can anyone explain it? 有人可以解释吗? Why its not modifying the value? 为什么不修改值? Is there way to change/modify the value as per Line? 有没有办法按行更改/修改值?

Thanks and Regards 谢谢并恭祝安康

According to MSDN ( TextBoxBase.Lines Property ): 根据MSDN( TextBoxBase.Lines属性 ):

By default, the collection of lines is a read-only copy of the lines in the TextBox. 默认情况下,行集合是TextBox中行的只读副本。 To get a writable collection of lines, use code similar to the following: textBox1.Lines = new string[] { "abcd" }; 要获得可写的行集合,请使用类似于以下代码:textBox1.Lines = new string [] {“ abcd”};

so you better go for: 所以你最好去:

for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
    if (richTextBox1.Lines[i] == "ok")
    {
        string[] lines = richTextBox1.Lines;
        lines[i] = "done";
        richTextBox1.Lines = lines;
    }
}

UPDATE: another way of doing this (that I do not recommend though): 更新:执行此操作的另一种方式(尽管我不建议这样做):

string line = richTextBox1.Lines[i]; 
richTextBox1.Find(line);
richTextBox1.SelectedText = "done";

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

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