PDA

View Full Version : RichTextBox Text Posistioning


JustAdotNETUser
05-16-2005, 05:58 PM
I have a RichTextBox that I'm using as a sort of log file display. I display some text then a newline and more text e.t.c. Sometimes I dont want a new line I want to print on the same line, anyone know how?

I want the RichTextBox equivelent of:

ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
ListBox1.Items.Add("My OutPut")

TIA

AtmaWeapon
05-17-2005, 10:11 AM
Take a look at the AppendText function.

Adding text to the current line:
RichTextBox.AppendText("text")

Adding text to a new line:
RichTextBox.AppendText(Environment.Newline & "text")

JustAdotNETUser
05-17-2005, 12:12 PM
Take a look at the AppendText function.

Adding text to the current line:
RichTextBox.AppendText("text")

Adding text to a new line:
RichTextBox.AppendText(Environment.Newline & "text")

No that's not what I meant......

I'm refering to replacing the last line, not appending to it.

AtmaWeapon
05-17-2005, 01:00 PM
Unless I am missing something simple, this is not an easy task.

My first idea was to write a function that alters the last member of the RichTextBox's Lines property, but it seems that attempts to alter the text through this property fail.

Unfortunately this means my next idea is the answer unless I am missing something obvious.

You could position the cursor at the end of the text, and determine the index of this character through the RichTextBox's Text property (it would be at position Text.Length, I think). Then, you would have a loop decrement this character position and use the RichTextBox's GetLineIndexFromChar method to determine what line you are on. When this line index changes, you know you have found the beginning of the line (the position after the index where the line changes). From this, it is simple to select the final line and replace its text with new text.

However, this function requires special attention when there is only one line, as the index will not change because there is no previous line to run into. In this case the first character position is always 0.