RichTextBox scrolling problem

U

Udi

Hi,
I need to update the last line of Lines in my derived RichTextBox.
Since I can't do this:

Lines[Lines.Length -1] = history[historyIdx] as string;

I need to copy 'Lines' to a tmp array, change it and copy it back to
'Lines':

:
tmpTxtMatrix = new string[....];
Lines.CopyTo(tmpTxtMatrix, 0);

//Replace last line in console with history value
tmpTxtMatrix[tmpTxtMatrix.Length - 1] = history[historyIdx] as string;

//Setting the new lines array
Lines = tmpTxtMatrix;
SelectionStart = this.TextLength;
this.ScrollToCaret();
:

My problem is that this procedure causes the textBox to scroll up to
the begining of it, and then scroll back down to the last line.
Is there a way to update the last line without copying all lines?
If not, how do I prevent the scroll side effect?
Thanks!
Udi
 
S

Stoitcho Goutsev \(100\)

Udi,

Here is an idea how to solve both your problems

int lastLineIndex = this.richTextBox1.Lines.Length - 1;
int lastLineFirstCharIndex =
this.richTextBox1.GetFirstCharIndexFromLine(lastLineIndex);
int lastLineLength = richTextBox1.SelectionStart =
richTextBox1.Lines[lastLineIndex].Length;

this.richTextBox1.SelectionStart = lastLineFirstCharIndex;
this.richTextBox1.SelectionLength = lastLineLength;
this.richTextBox1.SelectedText = "New Last Line";
this.richTextBox1.ScrollToCaret();
this.richTextBox1.SelectionStart = lastLineFirstCharIndex;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top