Not being able to insert text to a rich text box

K

K Viltersten

I'm doing this on my rich text box called rtb, with cmb being a combo box.

rtb.Text.Insert (rtb.Text.Length, cmb.SelectedItem.ToString () + "\n");

The text, however, doesn't appear. What do i miss?

The idea is to add the text selected from the combo box each
time the selection is made. That way, one can create a simple
log or "how-to" list of commands.
 
P

Peter Duniho

I'm doing this on my rich text box called rtb, with cmb being a combo
box.

rtb.Text.Insert (rtb.Text.Length, cmb.SelectedItem.ToString () + "\n");

The text, however, doesn't appear. What do i miss?

You didn't assign the result of the Insert() method back to the text box's
Text property.

Strings are immutable, so no String method will ever change the instance
you're executing the method on. It will always return a new instance.
That's the basic problem with the code you're using.

To further complicate matters more generally though: in addition to that,
your code assumes that the Text property returns the string instance
internal to the RichTextBox instance. Not all properties will do that.
If they return a mutable object, it's quite possible the class will create
a new copy of the object for you. Or it might not. It's up to the object.

So you should always be very careful about doing things to objects
returned from properties. There are several ways that things that look
like changes will turn out to not actually be changes.

Oh, and just to make things even more complicated, you should also beware
of doing things accidently to objects returned by properties where you
_didn't_ expect the instance used to get the property to be modified by
your code. :)

Pete
 
K

K Viltersten

Peter Duniho said:
You didn't assign the result of the Insert() method back to the text box's
Text property.

Strings are immutable, so no String method will ever change the instance
you're executing the method on. It will always return a new instance.
That's the basic problem with the code you're using.

To further complicate matters more generally though: in addition to that,
your code assumes that the Text property returns the string instance
internal to the RichTextBox instance. Not all properties will do that.
If they return a mutable object, it's quite possible the class will create
a new copy of the object for you. Or it might not. It's up to the
object.

So you should always be very careful about doing things to objects
returned from properties. There are several ways that things that look
like changes will turn out to not actually be changes.

Oh, and just to make things even more complicated, you should also beware
of doing things accidently to objects returned by properties where you
_didn't_ expect the instance used to get the property to be modified by
your code. :)


Got it. Thanks!
 

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