在 C# 中更改多行文本框的特定行 [英] Change Particular line of Multiline textbox in C#
问题描述
我无法更改多行文本框的特定字符串.
I am unable to Change the specific string of a multiline TextBox.
假设多行文本框的第一行是 "Hello"
&第二行是 "Bye"
.但是当我尝试更改第二行的值时,如下所示.
suppose first line of multiline textbox is "Hello"
& second line is "Bye"
.But when i trying to change the value of second line like below.
textBox1.Lines[1] = "再见";
当我使用调试模式看到结果时,它不是再见".
When I saw the result using Debug mode it was not "Good bye".
我也读过这个MSDN 文章&这个 stackoverflow 问题,但无法得到想要的回答.
I also read this MSDN article & this stackoverflow question but can't get the desired answer.
推荐答案
正如 MSDN 所述(您提供的链接):
As MSDN states (the link you provided):
默认情况下,行的集合是 TextBox 中行的只读副本.要获得可写的行集合,请使用代码类似于以下内容: textBox1.Lines = new string[] { "abcd" };
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" };
因此,您必须取"Lines
集合,对其进行更改,然后返回到TextBox
.可以这样实现:
So, you have to "take" Lines
collection, change it, and then return to TextBox
. That can be achieved like this:
var lines = TextBox1.Lines;
lines[1] = "GoodBye";
TextBox1.Lines = lines;
或者,您可以替换文本,例如Wolle 建议
Alternatively, you can replace text, like Wolle suggested
这篇关于在 C# 中更改多行文本框的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!