如何在多行文本框的一行中删除一个字符串#

how to remove a string in a line of Multiline textbox c#

我的表单中的多行文本框中有以下数据

我想要的是从该行删除“-”,我只想删除第一行但不更改其余文本框值

我试过但没有成功

Textbox1.Lines[0] = Textbox1.Lines[0].Replace(" -", "");

根据the documentationLines 属性:

Note

By default, the collection of lines is a read-only copy of the lines in the TextBox. To get a writable collection of lines, use code similar to the following: textBox1.Lines = new string[] { "abcd" };

所以,看来我们需要给它分配一个全新的数组,而不仅仅是修改现有的数组值。这样的事情应该可以解决问题:

var newLines = Textbox1.Lines;                // Capture the read-only array locally
newLines[0] = newLines[0].Replace(" -", "");  // Now we can modify a value
Textbox1.Lines = newLines;                    // And reassign it to our textbox