Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Particular line of Multiline textbox in C#

I am unable to Change the specific string of a multiline TextBox.

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] = "Good bye";

When I saw the result using Debug mode it was not "Good bye".

I also read this MSDN article & this stackoverflow question but can't get the desired answer.

like image 878
Neel Maheta Avatar asked Nov 26 '25 03:11

Neel Maheta


1 Answers

As MSDN states (the link you provided):

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" };

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;

Alternatively, you can replace text, like Wolle suggested

like image 96
Nino Avatar answered Nov 28 '25 15:11

Nino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!