New line in textbox display

TextBoxLines.Text += Environment.NewLine + "This is a new line"


Note though that strings in .NET are immutable. So adding to a string in fact creates a new string. You might take a look at the System.Text.StringBuilder class.
 
While the addition operator works in this situation it is more proper to use the concatenation operator:
VB.NET:
[COLOR=black]TextBoxLines.Text &= Environment.NewLine & [COLOR=maroon]"This is a new line"[/COLOR][/COLOR]
There are certain situations where using the addition operator can cause issues. Note that the TextBox class also supports the AppendText method:
VB.NET:
[COLOR=black]TextBoxLines.AppendText(Environment.NewLine & [/COLOR][COLOR=black][COLOR=maroon]"This is a new line"[/COLOR][/COLOR][COLOR=black])[/COLOR]
or:
VB.NET:
[COLOR=black]TextBoxLines.AppendText([/COLOR][COLOR=black]Environment.NewLine[/COLOR][COLOR=black])[/COLOR][COLOR=black]
[/COLOR][COLOR=black]TextBoxLines.AppendText([COLOR=maroon]"This is a new line"[/COLOR][/COLOR][COLOR=black])[/COLOR]
Note that this is not especially advantageous and is actually less efficient, but it does make your code clearer as it is self-documenting.
 
jmcilhinney said:
While the addition operator works in this situation it is more proper to use the concatenation operator...
I don't agree.(*) :)
I know the documentation (help) states otherwise, but as far as I'm concerned it's just plain wrong in this regard.
With Option Strict On both operators map to the String.Concat method if the operand is a System.String. The problem is that the & operator will automagically convert other type operands to System.String. This is done by calling Microsoft.VisualBasic.CompilerServices.Conversions.ToString, wich in turn will call some .ToString method on the operand.
I don't want these autoconversions, they often introduce errors that go unnoticed until runtime (which beats the purpose of using Option Strict On in the first place, right?).
jmcilhinney said:
There are certain situations where using the addition operator can cause issues...
Again, my point of view is that it's the & operator that can cause issues.
I'm not aware how the + operator could possibly cause issues (with Option Strict On, that is), because all operands must be of type System.String.

(*) Glad to, too. It was getting scary to read your comments and remarks in other topics and thinking each time "hey, that's exactly what I would've said". :D
 
With Option Strict turned Off, this code will throw an exception at run-time:
VB.NET:
Dim n As Integer = 10

MessageBox.Show("The number is " + n)
because it will interpret the addition operator as integer addition. It will fail becuase it cannot convert the String to an Integer. With the concatenation operator it will work regardless because, as you say, the Integer will be implicitly converted to a String. You know for a fact that you are concatenating strings. What possible reason could there be that that you wouldn't want the number converted to a String? It's akin to using the String.Format method, which also has no requirement that any object you use be a String. This is most likely to cause an issue for beginners, and it is mostly beginners that will be working with Option Strict turned Off. Thus they are the ones most likely to encounter these run time exceptions due to using the addition operator.
 
With Option Strict On, this code will not even compile. ;)
Programmers (even very knowledgeable and experienced ones) not always write the code they think they are writing. Strongly typed languages and constructs help us, programmers, to make sure we really write what we intend to.
So I still think your recommendation to use the & operator should read something like:
"While the addition operator works in this situation it is more proper to use the concatenation operator, if (and only if) Option Strict is Off ..."
"... and you should always set Option Strict On!" :D
So in my book, the + operator is the preferred one to use.
 
Last edited:
retkehing said:
How to display the text in next line of the textbox?

how about setting the textbox multiline property to true at design phase.

than you can add the multiline by code such as:

txtoutput.text &= vbcrlf & "The New Line"
 
hello,
this code go to a new line but i need a code stay line !
TextBoxLines.AppendText(Environment.NewLine & "This is a new line")
this code go to a new line , but i need code , i'm clicked for any line insert code to this line , thanks
 
or simply...

dim iTEXT as string = textLog.Text
textLog.Text = "A new line of text" & vbNewLine & iTEXT

edit...

that puts the new line on top...

textLog.Text = iTEXT & vbNewLine & "A new line of text"

puts on bottom.
 
Last edited:
Back
Top