87

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

user692942
  • 16,398
  • 7
  • 76
  • 175
Wasim A.
  • 9,660
  • 22
  • 90
  • 120

17 Answers17

112
  • 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
  • 23,794
  • 27
  • 122
  • 225
Fun Mun Pieng
  • 6,751
  • 3
  • 28
  • 30
  • 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 - on strike 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 - on strike 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`) – Galacticai Apr 13 '21 at 21:26
34

Try using vbcrlf for a newline

msgbox "This is how" & vbcrlf & "to get a new line"
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Developer
  • 8,390
  • 41
  • 129
  • 238
  • 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 - on strike
  • 239,200
  • 50
  • 490
  • 574
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • @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 - on strike 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 - on strike Mar 01 '11 at 08:46
17

Use the Environment.NewLine property

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jens
  • 3,249
  • 2
  • 25
  • 42
11

Add a vbNewLine as:

"text1" & vbNewLine & "text2"
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
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
  • 318
  • 5
  • 10
4

The correct format is :

"text1" + vbNewLine + "text2"
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
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
  • 4
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,851
  • 2
  • 17
  • 22
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