Messagebox: creating a new line

noosaj

Member
Joined
Mar 5, 2007
Messages
6
Programming Experience
Beginner
Hello,

I am developing a small windows program as part of my project requirement in school.

What I'm trying to do is calculate information based on the user input, and display all information in a MessageBox.

I have figured out everything BUT one thing. How do I create a new line in the MessageBox??

For example:

The output that I have shows: Stockbroker: Wilson Stock Name: Wilson Total Shares: 4, etc...

I want the messagebox to display each line as a separate line, not all on one line.

How?? Many thanks for your help!
 
Environment.NewLine
Messagebox.Show("Stockbroker: Wilson" & Environment.NewLine & "Stock Name: Wilson" & Environment.NewLine & "Total Shares: 4")
 
also:

VB.NET:
Dim sb as New StringBuilder
sb.AppendLine("Stockbroker: Wilson")
sb.AppendLine("Stock Name: Wilson")
sb.AppendLine("Shares: 4")
 
MessageBox.Show(sb.ToString())


What Im angling at teaching here is not so much that this is the way you must do multiple line strings, but more that if you have strings that you build up over time, or are always changing, you should use a stringbuilder 9instead, to create them, and then ToString() it whenever you want to show the string you have built..
 
Last edited by a moderator:
Building upon cjard's lesson there, the reason for this is that strings are what is called "immutable". Meaning they cannot change. Once they are set, that's it. You are probably now going "Wait a minute! But... But..." When you concatenate strings together, what really happens is that a NEW COPY of the string is created with the appended text to it. So, given this:
VB.NET:
Dim MyString As String
MyString = "This"
MyString = MyString & " and That"
MyString = MyString & " and The Other"

Here's what happens. "This" gets copied over to memory and the address is placed into MyString. On the next line, it taks "This" adds " and That" to it, and copies it to a new memory location, and places that address in MyString. On the third line, it takes "This and That" adds " and The Other" to it, again copies it to a new location and again places that address in MyString.

Ok, so what if we do the concatenation all on one line, like this:
VB.NET:
Dim MyString As String
MyString = "This" & " and That" & " and The Other"

Here's the punch line: It does the exact same thing. You can quickly see how this can lead to performance problems.

That's where the StringBuilder comes in. It removes that requirement of having strings copied around. I'll be honest, I don't know how it does it, but it works. I've seen strings concatenated 1000 times take 14 seconds. But when we concatenated 1,000,000 (yes, a million) items using string builder - took 9.2 seconds!

-tg
 
You can also insert this into your code: (example)
VB.NET:
msgboxstring = "This" & chr(13) &  " and That" & chr(13) & " and The Other"
the chr(13) is a carriage return causing a new line.

old school asc programming.
 
i tend to use vbNewLine as my return symbol:

Dim sMsg as string = "this is" + vbnewline + "over many lines"

ick, that's the VB6 compatibly talking there.... preferred method is Environment.NewLine.

You can also insert this into your code: (example)
VB.NET:
msgboxstring = "This" & chr(13) &  " and That" & chr(13) & " and The Other"
the chr(13) is a carriage return causing a new line.

old school asc programming.
Dude, that's so old school... welcome to the 2000's! And FYI... a carriage return simply returns to the front of the line.... well, it's supposed to... it's the newline (chr(10)) that actually moves to the new line (but is supposed to stay at current position)... that's why it's called a CR/LF. The CR simply send the tpying machine back to the begining of the line, while the LF scrolled one line.

-tg
 
I use ControlChars.NewLine or vbCrLf for new lines when doing simple concatenation

ie string = "one: " & vbCrLf & "1"
for:
one:
1
 
umn ok so that works as well. But for old school It' still works and well it seems to be taking up less space than your method.

Also Dude..... Could you be a bit more friendly when correcting someone thats trying to help someone else instead of talking down to them. I mean after all I did say it was old school, and I did say I was old school.
 
ok, let's keep our cool here

and yes there are several ways to get a new line in vb.net

the microsoft preferred way is: Environment.NewLine or ControlChars.NewLine but vb.net also supports:
vbnewline and vbcrlf

which helps when upgrading an old project to .Net, but the problem with vbcrlf is that it's not self documenting, for a person coming out of school only knowing vb.net has a high chance of not knowing exactly what vbcrlf does just by looking at it, whereas Environment.NewLine tells the person that a newline will be inserted in the string and because the call is in the Environment namespace you can count on the .Net Framework to know what the operating system's NewLine character is, on a Linux machine: vbnewline and vbcrlf would cause the program to crash

so just because something works because of backwards compatibility doesn't necessarily mean it's good practice

also using the '+' operator in vb5 and higher is also considered bad practice, the '&' operator should be used to concatenate strings:
Dim myString As String = "This " + "is " + "bad!"
Dim myString As String = "This " & "is " & "good!"
 
umn ok so that works as well. But for old school It' still works and well it seems to be taking up less space than your method.

Also Dude..... Could you be a bit more friendly when correcting someone thats trying to help someone else instead of talking down to them. I mean after all I did say it was old school, and I did say I was old school.

Thanks for reinforcing a long held belief about this board. I shan't bother it again with my drivel.

-tg
 
so which way will be working with windows, linux or unix

whic is the best one which i write once and run every where
not write once and debug every where
 
haha I didn't realize that was a concern for .net programmers..

a lot of .net functionalities will not work on *nix, so this and the rest of your cross-compatibility issues will probably be up to your own testing :)
 
Back
Top