84

What is the alternative to \n (for new line) in a MsgBox()?

user692942
  • 15,667
  • 7
  • 74
  • 164
Wasim A.
  • 9,201
  • 21
  • 89
  • 117

18 Answers18

106
  • for VB: vbCrLf or vbNewLine
  • for VB.NET: Environment.NewLine or vbCrLf or Constants.vbCrLf

Info on VB.NET new line: http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx

The info for Environment.NewLine came from Cody Gray and J Vermeire

Stevoisiak
  • 20,148
  • 23
  • 110
  • 201
Fun Mun Pieng
  • 6,501
  • 3
  • 27
  • 29
  • 12
    Do not use `vbCrLf` for VB.NET code! That's only there for compatibility with ported VB 6 applications. You should be using `Environment.NewLine` instead, as suggested in J. Vermeire's answer. – Cody Gray Mar 01 '11 at 08:55
  • I agree with you that Environment.NewLine should be used, but because it is cross platform. It returns \r\n for Window, but only \n for unix/linux based platforms. – Fun Mun Pieng Mar 02 '11 at 05:00
  • Yes, that's a very good reason to use it. Considering that alternative implementations of the CLR (like Mono) support VB.NET, it's a good idea to write code with an eye towards platform independence. Beyond that, adopting standard .NET Framework idioms, rather than holdovers for backwards compatibility purposes, is always a good idea. The biggest mistake any VB.NET programmer will make is assuming it's the same language as VB 6. **It's emphatically not!** The true object-orientation is only scratching the surface of the differences. Pretending otherwise is doing yourself an injustice. – Cody Gray Mar 02 '11 at 05:39
  • 2
    Down vote because it doesn't work for me. Calling EditPoint.Insert(vbNewLine) in a VB macro inserts \r\n. vbLf is the correct answer to the asked question. – Samuel Nov 27 '12 at 18:16
  • 1
    I just declared a public string called `n`... just for convenience. (`Public n As String = Environment.NewLine`) – XEROling Apr 13 '21 at 21:26
33

Try using vbcrlf for a newline

msgbox "This is how" & vbcrlf & "to get a new line"
Stevoisiak
  • 20,148
  • 23
  • 110
  • 201
Developer
  • 8,228
  • 37
  • 122
  • 230
  • 1
    The example is appreciated because I don't work with VB and didn't know the concatenation format. They are evidently not interpreted within double quotes. – Tim Morton Aug 14 '20 at 21:46
24

These are the character sequences to create a new line:

  • vbCr is the carriage return (return to line beginning),

  • vbLf is the line feed (go to next line)

  • vbCrLf is the carriage return / line feed (similar to pressing Enter)

I prefer vbNewLine as it is system independent (vbCrLf may not be a true new line on some systems)

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
Pranay Rana
  • 170,430
  • 35
  • 234
  • 261
  • @Andrew - during my college days when i do programming in vb – Pranay Rana Mar 01 '11 at 08:37
  • 1
    Theoretically, I suppose you're right. But in every implementation that I've seen, `vbNewLine` is simply defined as `vbCrLf`. There isn't any difference. – Cody Gray Mar 01 '11 at 08:42
  • I wouldn't change anything. I agree with you the way it is. I think `vbNewLine` more clearly expresses your intent. It's obviously been *designed* to insert a new line. I was just providing auxiliary commentary. "Under the hood", they do the same thing. Implementation details like that shouldn't leak into your code. – Cody Gray Mar 01 '11 at 08:46
17

Use the Environment.NewLine property

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
Jens
  • 3,211
  • 2
  • 24
  • 41
  • This is the correct solution for VB.NET. Skip the `vbCrLf`/`vbNewLine` nonsense unless you're using VB 6. – Cody Gray Mar 01 '11 at 08:42
10

Add a vbNewLine as:

"text1" & vbNewLine & "text2"
Hans Olsson
  • 53,038
  • 14
  • 91
  • 113
5

An alternative to Environment.NewLine is to use :

Regex.Unescape("\n\tHello World\n")

from System.Text.RegularExpressions

This allows you to escape Text without Concatenating strings as you can in C#, C, java

slagou
  • 308
  • 5
  • 10
4

The correct format is :

"text1" + vbNewLine + "text2"
Paul Roub
  • 35,848
  • 27
  • 79
  • 88
2

Use the command "vbNewLine"

Example

Hello & vbNewLine & "World"

will show up as Hello on one line and World on another

SQAHero
  • 41
  • 1
  • 3
2

You can use Environment.NewLine OR vbCrLF OR vbNewLine

MsgBox($"Hi!{Environment.NewLine}I'M HERE")
zdlk
  • 21
  • 3
1

You can use carriage return character (Chr(13)), a linefeed character (Chr(10)) also like

MsgBox "Message Name: " & objSymbol1.Name & Chr(13) & "Value of BIT-1: " & (myMessage1.Data(1)) & Chr(13) & "MessageCount: " & ReceiveMessages.Count
D_T
  • 33
  • 4
1
Module MyHelpers
    <Extension()>
    Public Function UnEscape(ByVal aString As String) As String

       Return Regex.Unescape(aString)

    End Function
End Module

Usage:

console.writeline("Ciao!\n".unEscape)
1

On my side I created a sub MyMsgBox replacing \n in the prompt by ControlChars.NewLine

Bertrand
  • 11
  • 1
1

A lot of the stuff above didn't work for me. What did end up working is

Chr(13)
Ariel Gabizon
  • 2,341
  • 1
  • 15
  • 21
1
msgbox "This is the first line" & vbcrlf & "and this is the second line"

or in .NET msgbox "This is the first line" & Environment.NewLine & "and this is the second line"

1

do not forget to set the Multiline property to true in textbox

0

msgbox("your text here" & Environment.NewLine & "more text") is the easist way. no point in making your code harder or more ocmplicated than you need it to be...

user3174223
  • 31
  • 2
  • 7
  • 1
    Your answer (`Environment.NewLine`) was already given 3 years ago and is the highest voted answer. This is more like commentary. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have [sufficient reputation](http://stackoverflow.com/help/privileges/comment) you will be able to comment on any post. – Scott Solmer Nov 18 '14 at 17:07
0

This work for me: MessageBox.Show("YourString" & vbcrlf & "YourNewLineString")

-1

The message box must end with a text and not with a variable