newline format control in richtextbox

G

Guest

I can't seem to add a newline (/n) to get a richtextbox control to display text on successive lines. The text that I type is overwritten. How do I remedy this

My example

richTextBox->Multiline = true
richTextBox->Text = S"First line /n"
richTextBox->Text = S"Second line"

Thanks

DAS
 
S

Steve McLellan

Hi,

Try using \r\n - sounds like you're just getting a carriage return, and not
a newline.

Steve

D Steward said:
I can't seem to add a newline (/n) to get a richtextbox control to display
text on successive lines. The text that I type is overwritten. How do I
remedy this?
 
G

Guest

I have tried numerous combinations of escape sequences, and none of them work

It appears what happens is that it doesn't recognize the escape sequence and just overwrites the previous line

Any suggestions would be appreciated

DAS
 
A

Austin Ehlers

Also use the += operator. Otherwise you're just overwriting the
previous string.

Austin
 
G

Guest

I tried this to no avail. Could you give me an example of how I could modify my originally posted code

Thanks

DA

----- Austin Ehlers wrote: ----

Also use the += operator. Otherwise you're just overwriting th
previous string

Austi

On Fri, 30 Jan 2004 12:43:37 -0000, "Steve McLellan
 
H

harry_bosch

=?Utf-8?B?RCBTdGV3YXJk?= said:
It appears what happens is that it doesn't recognize the escape
sequence and just overwrites the previous line.

You're not using an escape sequence, that's why :)

"/n" vs. "\n"
 
H

harry_bosch

=?Utf-8?B?RCBTdGV3YXJk?= said:
You are correct that this was not an escape sequence as I mistakenly
wrote it here. It is written correctly in my program, however as "\n".
This still doesn't solve the problem. Austin Ehlers added that I
should use += somewhere, but where? This should not be an
extraordinarily difficult problem in a richtextbox control, but
appears to be difficult indeed.

Okay, we got the escape thing out of the way then.

If these next two lines are what you have in your program...

richTextBox->Text = S"First line /n";
richTextBox->Text = S"Second line";

....then we see that the second line is assigning a new string to the Text
property, which overwrites the effects of the first line. The second line
should use '+=' in order to append text instead of overwriting it.
 
G

Guest

Harry

I assume that you mean

richTextBox->Text = S"First line /n"
richTextBox->Text += S"Second line"

I tried this and it doesn't compile because it is a managed object where pointer arithmetic is not allowed

DA

----- harry_bosch wrote: ----

=?Utf-8?B?RCBTdGV3YXJk?= said:
You are correct that this was not an escape sequence as I mistakenl
wrote it here. It is written correctly in my program, however as "\n"
This still doesn't solve the problem. Austin Ehlers added that
should use += somewhere, but where? This should not be a
extraordinarily difficult problem in a richtextbox control, bu
appears to be difficult indeed.

Okay, we got the escape thing out of the way then

If these next two lines are what you have in your program..

richTextBox->Text = S"First line /n"
richTextBox->Text = S"Second line"

....then we see that the second line is assigning a new string to the Text
property, which overwrites the effects of the first line. The second line
should use '+=' in order to append text instead of overwriting it
 
H

harry_bosch

=?Utf-8?B?RCBTdGV3YXJk?= said:
Harry,

I assume that you mean:

richTextBox->Text = S"First line /n";
richTextBox->Text += S"Second line";

I tried this and it doesn't compile because it is a managed object
where pointer arithmetic is not allowed.

Hmm, too much native C++ for me lately :) This is managed code, so String
is immutable. I would then use StringBuilder to create the complete string,
and then assign it to the Text property (if, for instance, in your real
system you are doing more than just these two short lines).

I haven't checked the syntax on this, but you could also just assign to
Text the concatenation of its current value with the new text you want to
append. Something like:

richTextBox->Text = richTextBox->Text + S"Second line";

I would opt for a StringBuilder, however, instead of this, unless testing
has shown that the difference is negligible.

A quick look in the .NET docs shows that RichTextBox (via its base class)
has a Lines property, which allows you to assign lines to a multiline
control by index. Then you could do something like:

richTextBox->Lines[0] = S"My first line";
richTextBox->Lines[1] = S"My second line";

I've never used these classes, so the above is completely untested.
 
G

Guest

Harry,

Thanks for the help. However, I found another route that works fine. Instead of using richTextBox1->Text ... I use richTextBox1->AppendText( S"\n First line \n second line"). This works fine for me.

So, now I go on with my project.

DAS

----- harry_bosch wrote: -----

=?Utf-8?B?RCBTdGV3YXJk?= said:
Harry,
richTextBox->Text += S"Second line";
where pointer arithmetic is not allowed.

Hmm, too much native C++ for me lately :) This is managed code, so String
is immutable. I would then use StringBuilder to create the complete string,
and then assign it to the Text property (if, for instance, in your real
system you are doing more than just these two short lines).

I haven't checked the syntax on this, but you could also just assign to
Text the concatenation of its current value with the new text you want to
append. Something like:

richTextBox->Text = richTextBox->Text + S"Second line";

I would opt for a StringBuilder, however, instead of this, unless testing
has shown that the difference is negligible.

A quick look in the .NET docs shows that RichTextBox (via its base class)
has a Lines property, which allows you to assign lines to a multiline
control by index. Then you could do something like:

richTextBox->Lines[0] = S"My first line";
richTextBox->Lines[1] = S"My second line";

I've never used these classes, so the above is completely untested.
 
H

harry_bosch

=?Utf-8?B?RCBTdGV3YXJk?= said:
Thanks for the help. However, I found another route that works fine.
Instead of using richTextBox1->Text ... I use
richTextBox1->AppendText( S"\n First line \n second line"). This works
fine for me.

Glad to hear it. I missed that one -- I guess reading the docs really does
pay off, huh? :)
 

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