【问题标题】:how to remove a string in a line of Multiline textbox c#如何在多行文本框c#的一行中删除一个字符串
【发布时间】:2020-05-04 22:24:58
【问题描述】:

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

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

我尝试过但没有成功

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

【问题讨论】:

  • textBox1.Text = textBox1.Text.Replace(textBox1.Lines[0] + "\r\n", textBox1.Lines[0].Replace(" -", "") + "\r\n"); 可以,但要注意重复。

标签: c# string textbox multiline


【解决方案1】:

根据the documentationLines 属性:

注意

默认情况下,行集合是TextBox 中行的只读副本。要获得可写的行集合,请使用类似于以下的代码: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多