Hi there
I need one message box with two lines. How can i do that?
Thank you
Printable View
Hi there
I need one message box with two lines. How can i do that?
Thank you
Moved From The CodeBank
Code:MessageBox.Show("First line." & ControlChars.NewLine & "Second line.")
Code:MessageBox.Show("This" & Environment.NewLine & "is" & Environment.NewLine & "a test", "Lines", MessageBoxButtons.YesNoCancel)
Hi,
This is another way to do it;
vb Code:
MessageBox.Show("This is your fist line," & vbNewLine & "this your second line.")
This is also working like the others but what's the difference between all three!
Wkr,
sparrow1
http://msdn.microsoft.com/en-us/libr...h0(VS.80).aspxQuote:
Originally Posted by sparrow1
Is vbNewLine the same as vbCrLf? I always use the latter. According to that link they are both Chr(13)+Chr(10). Just wondering why there are two constants for the same thing...Quote:
Originally Posted by JuggaloBrotha
see remarks here
http://msdn.microsoft.com/en-us/libr...t.newline.aspx
for why i use Environment.NewLine
Don't use either, use Environment.NewLine instead.Quote:
Originally Posted by nbrege
Juggalo ... why ?
I have always used vbCrLF (chr(13) + chr(10)) so I am just wondering what the percieved advantage of Environment.NewLine is, from what I can tell with the following code
Environment.Newline just returns chrs 13 and 10 (although this may change on other machines I suppose)Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lString As String = ""
For Each l As Char In Environment.NewLine
lString += "," & Asc(l)
Next
MsgBox(lString)
End Sub
The environment class is there to allow you to use things based on the environment (the OS) that your app is running on. For instance, ControlChars.NewLine, vbNewLine, vbCrLf, vbLf should all fail in your .Net applications when it's running on Mono (Linux/Mac) because all you need is the Cr (Carrage Return) and not the Lf (Line Feed) for a new line, whereas in windows it uses both Cr and Lf for a new line. So on Windows Environment.NewLine gives you the CrLf combination whereas the same Environment.NewLine on Mono gives you just the Cr.Quote:
Originally Posted by SpaceMonkeys
In short, Environment.NewLine reduces the need to check for what OS your app is running on because that's already defined in the Environment class.
And a quote from MSDN:http://msdn.microsoft.com/en-us/libr...t.newline.aspxQuote:
Property Value
Type: System.String
A string containing "\r\n" for non-Unix platforms,
or
a string containing "\n" for Unix platforms.
Juggalo ... makes perfect sense. But if we only develope Windows apps then it shouldn't make a difference one way or the other, right?
Cool, that's what I though you were going to say (coming from a C++ background) just wanted to be sure, if your going to be developing windows only apps yeah either will be fine .... but good practice is good practice
Yes, but what if you're suddenly told by your boss that your app is going to be modified to run on Mono as well as Windows and tells you to make the changes by the end of the week, would you want to spend a day or more changing all of the vbCrLf's and vbNewLine's and ControlChar.Newline's to Environment.NewLine?Quote:
Originally Posted by nbrege
I wouldn't.
I personally have a shared routine that I include in every app I work on called NL
It looks like this:
It is usually sitting in a module in a linked file to my project so it is easy to reuse. You could also put it in a class and reference it as a DLL though with other support functions you may make and use frequently in many projects.Code:Public Shared Function NL(Optional ByVal Count As Integer = 1) As String
'HOLDS OUR NEWLINES
Dim temp As String = String.Empty
'IF USER PASSED IN NEGATIVE OR 0, MAKE 1
If Count < 1 Then Count = 1
'LOOP TO MAKE AS MANY NEW LINES AS WE WANT
For i As Integer = 1 To Count
temp &= Environment.NewLine
Next
'RETURN VALUE
Return temp
End Function
Anyway, the reason I made a function, is because then when I want a newline all I have to type is NL
But if I want 2 blank lines, I can also doCode:Dim x as string = "Hello" & NL & "World"
'Result:
'Hello
'World
etc....Code:Dim x as string = "Hello" & NL(2) & "World"
'Result:
'Hello
'
'World
Since the param is optional, it defaults to 1 line when you don't pass an argument. It also would make this very easy to change if I decided for some reason I did want to use vbCrLf or something like that instead of Environment.NewLine (or maybe I want to change to use a stringbuilder object instead of a temp string variable to build my newlines). If I ever did want to change the functionality, I could just change it in the function, and not have to update it in 50 million spots in my app.
oh, and just one additional comment. I noticed when I had formatted some messages for a messagebox to be multiline, and it looked fine on XP, I noticed the same messagebox would actually have line breaks in different spots on Vista.
I guess they changed the text area of messageboxes in vista, with relation to the width a messagebox will get to based on its text before it forces a newline. So if you format your messageboxes with newlines to look all nice in 1 OS, it may look like crap in another, so make sure to test.
Quote:
Originally Posted by kleinma
Instead of a loop you could use this:
It might be a little more efficient. Or maybe not. Nice routine BTW... :bigyello:Code:temp = StrDup(Count, Environment.NewLine)
StrDup duplicates the first character in a string, so I have not tested, as I don't use it, but environment.newline is a cr and lf as stated above for Windows machines, so there is a good change using StrDup would return ONLY a string of CR characters, and no LF characters.
From Help:
StrDup
This function returns a String made up of repeated characters. The character that makes up the string is the first character in the Character argument, and it is duplicated Number number of times.
Code:Dim aString As String = "Wow! What a string!"
' Returns "WWWWWWWWWW"
TestString = StrDup(10, aString)
Oops .. my bad. I should have tested before posting. :o
Hi All,
Thanks for the explanations, I'll use it in the future.
Wkr,
sparrow1