Results 1 to 10 of 10

Thread: New line in textbox display

  1. #1
    retkehing is offline VB.NET Forum Genius
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Posts
    153
    Reputation
    112

    New line in textbox display

    How to display the text in next line of the textbox?

  2. #2
    Don Delegate is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    140
    Reputation
    112
    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.

  3. #3
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,995
    Reputation
    1627
    While the addition operator works in this situation it is more proper to use the concatenation operator:
    Code:
    TextBoxLines.Text &= Environment.NewLine & "This is a new line"
    There are certain situations where using the addition operator can cause issues. Note that the TextBox class also supports the AppendText method:
    Code:
    TextBoxLines.AppendText(Environment.NewLine & "This is a new line")
    or:
    Code:
    TextBoxLines.AppendText(Environment.NewLine)
    TextBoxLines.AppendText("This is a new line")
    Note that this is not especially advantageous and is actually less efficient, but it does make your code clearer as it is self-documenting.

  4. #4
    Don Delegate is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    140
    Reputation
    112
    Quote Originally Posted by jmcilhinney
    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?).
    Quote Originally Posted by jmcilhinney
    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".

  5. #5
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,995
    Reputation
    1627
    With Option Strict turned Off, this code will throw an exception at run-time:
    Code:
    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.

  6. #6
    Don Delegate is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    140
    Reputation
    112
    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!"
    So in my book, the + operator is the preferred one to use.
    Last edited by Don Delegate; 02-12-2006 at 10:11 AM.

  7. #7
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,995
    Reputation
    1627

  8. #8
    hadinatayp is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Feb 2006
    Posts
    95
    Reputation
    110
    Quote Originally Posted by retkehing
    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"

  9. #9
    Sara Jay Sander is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Posts
    1
    Reputation
    0
    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

  10. #10
    s1ckOh is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    San Diego, Ca
    Posts
    68
    Reputation
    49
    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 by s1ckOh; 08-13-2011 at 8:34 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking