How to use \n new line in VB msgbox() ...?
426,970
Solution 1
-
for VB:
vbCrLf
orvbNewLine
-
for VB.NET:
Environment.NewLine
orvbCrLf
orConstants.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
Solution 2
Try using vbcrlf
for a newline
msgbox "This is how" & vbcrlf & "to get a new line"
Solution 3
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)
Solution 4
Use the Environment.NewLine
property
Solution 5
Add a vbNewLine
as:
"text1" & vbNewLine & "text2"
Related videos on Youtube
Author by
Wasim A.
[email protected] | Skype:wasimxe | Whatsapp: +923455407008
Updated on February 20, 2022Comments
-
Wasim A. over 2 years
What is the alternative to
\n
(for new line) in aMsgBox()
?-
Cody Gray over 13 yearsAre you asking about VB.NET or VB 6?
-
Peter Mortensen about 10 yearsSee also End-of-line identifier in VB.NET?.
-
user692942 over 2 years@PeterMortensen who decided this question should be changed to VB.Net?
-
-
Pranay Rana over 13 years@Andrew - during my college days when i do programming in vb
-
Cody Gray over 13 yearsTheoretically, I suppose you're right. But in every implementation that I've seen,
vbNewLine
is simply defined asvbCrLf
. There isn't any difference. -
Cody Gray over 13 yearsThis is the correct solution for VB.NET. Skip the
vbCrLf
/vbNewLine
nonsense unless you're using VB 6. -
Cody Gray over 13 yearsI 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 over 13 yearsDo not use
vbCrLf
for VB.NET code! That's only there for compatibility with ported VB 6 applications. You should be usingEnvironment.NewLine
instead, as suggested in J. Vermeire's answer. -
Fun Mun Pieng over 13 yearsI 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.
-
Cody Gray over 13 yearsYes, 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.
-
Samuel over 11 yearsDown 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.
-
Scott Solmer over 9 yearsYour 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 you will be able to comment on any post. -
Lenard Bartha about 4 yearsShouldn't it be Chr(10)?
-
Tim Morton almost 4 yearsThe 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.
-
Marcucciboy2 over 3 years@LenardBartha likely not stackoverflow.com/a/38792168/2727437
-
XEROling about 3 yearsI just declared a public string called
n
... just for convenience. (Public n As String = Environment.NewLine
) -
user692942 over 2 yearsWhich is equivalent to the
vbCr
built in constant.