What is the alternative to \n (for new line) in a MsgBox()?
- 15,667
- 7
- 74
- 164
- 9,201
- 21
- 89
- 117
-
1Are you asking about VB.NET or VB 6? – Cody Gray Mar 01 '11 at 08:42
-
See also *[End-of-line identifier in VB.NET?](http://stackoverflow.com/questions/1399268)*. – Peter Mortensen Apr 09 '14 at 00:18
-
1@PeterMortensen who decided this question should be changed to VB.Net? – user692942 Feb 19 '22 at 22:22
18 Answers
- for VB:
vbCrLforvbNewLine - for VB.NET:
Environment.NewLineorvbCrLforConstants.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
- 20,148
- 23
- 110
- 201
- 6,501
- 3
- 27
- 29
-
12Do 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
-
2Down 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
-
1I just declared a public string called `n`... just for convenience. (`Public n As String = Environment.NewLine`) – XEROling Apr 13 '21 at 21:26
Try using vbcrlf for a newline
msgbox "This is how" & vbcrlf & "to get a new line"
- 20,148
- 23
- 110
- 201
- 8,228
- 37
- 122
- 230
-
1The 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
These are the character sequences to create a new line:
vbCris the carriage return (return to line beginning),vbLfis the line feed (go to next line)vbCrLfis 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)
- 230,875
- 49
- 477
- 553
- 170,430
- 35
- 234
- 261
-
-
1Theoretically, 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
Use the Environment.NewLine property
-
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
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
- 308
- 5
- 10
Use the command "vbNewLine"
Example
Hello & vbNewLine & "World"
will show up as Hello on one line and World on another
- 41
- 1
- 3
You can use Environment.NewLine OR vbCrLF OR vbNewLine
MsgBox($"Hi!{Environment.NewLine}I'M HERE")
- 21
- 3
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
- 33
- 4
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)
- 11
- 1
On my side I created a sub MyMsgBox replacing \n in the prompt by ControlChars.NewLine
- 11
- 1
A lot of the stuff above didn't work for me. What did end up working is
Chr(13)
- 2,341
- 1
- 15
- 21
-
-
@LenardBartha likely not https://stackoverflow.com/a/38792168/2727437 – Marcucciboy2 Oct 22 '20 at 14:48
-
Which is equivalent to the `vbCr` [built in constant](https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/miscellaneous-constants). – user692942 Feb 21 '22 at 13:38
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"
- 35
- 6
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...
- 31
- 2
- 7
-
1Your 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