graham23s
graham23s

Reputation: 385

Multiline TextBox Move Last Line Up 1

What i am trying to do is load in the contents of a multiline Text box to a method, which is done, that is easy enough, what i need to achieve is to move the last line of the textbox up by 1 place (or reverse the last line with the second last line)

Code:

        public void MoveLastLineUpOne(TextBox txtBox) {
            for (int i = 0; i < txtBox.Lines.Length; i++) {

            }
        }

The actual reversing of the last 2 lines is the part i am having issues with, i have never manipulated the Textbox values much in the past, i have tried to google the issue, from reading i think i need an insert (i think) at one point, i am more than likely over thinking this, any help would be appreciated.

Upvotes: 1

Views: 255

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

Have you checked out these solution here?

Bear in mind especially that:

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 something like the following should do the trick:

var originalLines = txtBox.Lines;
// Swap lines here
var temp = originalLines[originalLines.Length - 1];
originalLines[originalLines.Length - 1] = originalLines[originalLines.Length - 2];
originalLines[originalLines.Length - 2] = temp;
txtBox.Lines = originalLines;

Upvotes: 1

Related Questions