adding text to multiline textbox without erasing previous contents
how do i add text to a multiline textbox without erasing what is already in there?
Lets say the textbox already has...
"Visual basic is "
and then i want to add the word "fun" to the textbox without erasing "Visual basic is "
after adding "fun" i want it to read....
"Visual basic is fun"
any ideas?
Re: adding text to multiline textbox without erasing previous contents
well... think of your text box as a variable... if you have "Visual Basic 6 is " in a variable and you want to concatenate text to it, how would you go about doing that?
-tg
Re: adding text to multiline textbox without erasing previous contents
I had a similar problem while trying to add text in my Text Adventure game...
What I did was, whenever I want the game to add something new to the textbox without reasing the old thing I'd put:
textbox1.text = textbox1.text + " fun"
Hope this is what you were looking for.
Re: adding text to multiline textbox without erasing previous contents
vb Code:
textbox1.text &= "new text"
to put "new text" on a new line:
vb Code:
textbox1.text &= environment.newline & "new text"
Re: adding text to multiline textbox without erasing previous contents
I wasn't familiar with &= I would have coded it like
Code:
textbox1.Text = textbox1.Text & "new text"
I see that & and + are interchangeable. So you could code it
Code:
textbox1.text &= "new text" ' Or
textbox1.text += "new text" ' And
textbox1.text = textbox1.Text & "new text" ' is the same thing as
textbox1.text = textbox1.Text + "new text"
At least they're the same as far as I can see but maybe there's some behavioral difference under certain circumstances. I noticed that you can also put + and & in the same equation without a problem which makes sense since they do the same thing.
Code:
textbox1.text += "new text" & " some other text" + " and still more text"
Re: adding text to multiline textbox without erasing previous contents
+ is also used for addition. so with option strict off these 2 strings:
"10" & "10" = "1010"
"10" + "10" = 20
Re: adding text to multiline textbox without erasing previous contents
If you want to append text to a TextBox then call the AppendText method of that TextBox.
Re: adding text to multiline textbox without erasing previous contents
HOOORAY!
I got it to work using TextBox2.Text &= Environment.NewLine & "entered my stuff here" :)
Thanks all for the help and ideas.
Re: adding text to multiline textbox without erasing previous contents
Yippee. Now do it the proper way using the AppendText method like jmc told you to!!!! I can't believe the number of people, including some very advanced coders, who seem to be completely ignorant of this method!
Re: adding text to multiline textbox without erasing previous contents
considering that james' posting was almost exactly two years ago... I'm not surprised... people often just find the first thing that works and stop reading.
-tg
Re: adding text to multiline textbox without erasing previous contents
Quote:
Originally Posted by
dunfiddlin
Yippee. Now do it the proper way using the AppendText method like jmc told you to!!!! I can't believe the number of people, including some very advanced coders, who seem to be completely ignorant of this method!
there are often several ways to skin a cat, + in this case both are proficient uses of the language.
Re: adding text to multiline textbox without erasing previous contents
Quote:
Originally Posted by
.paul.
there are often several ways to skin a cat, + in this case both are proficient uses of the language.
They don't produce quite the same behaviour though and, generally speaking, the behaviour of AppendText is more desirable. I wouldn't call setting the Text property wrong though.