5 Replies - 348 Views - Last Post: 02 December 2013 - 04:18 PM Rate Topic: -----

#1 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

can you post more than one string in a messageBox?

Posted 01 December 2013 - 05:13 PM

public void button3_Click(object sender, EventArgs e)
        {
            Pet name = new Pet();
            Pet type = new Pet();
            Pet age = new Pet();

            name.name = textBox1.Text;
            type.name = textBox2.Text;
            age.name = textBox3.Text;

            MessageBox.Show("Your pet's name is " + name.name, "Your pet's type is " + type.name, "Your pet's age is " + age.name)
            MessageBox.Show("Your pet's type is " + type.name);
            MessageBox.Show("Your pet's age is " + age.name);
        }


Hey fellas! I was just curious if there is a way to display in one text box the following three stings instead of having three separate message boxes pop up? Yes i'm well aware i'm a noob XD

Is This A Good Question/Topic? 0
  • +

Replies To: can you post more than one string in a messageBox?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3458
  • View blog
  • Posts: 11,707
  • Joined: 12-December 12

Re: can you post more than one string in a messageBox?

Posted 01 December 2013 - 05:28 PM

You need to introduce newlines into the MessageBox prompt:

MessageBox.Show("this is first line\nthis is second line");
// or
MessageBox.Show("this is first line" + Environment.NewLine + "this is second line");

You can also use String.Format or String.Concat to build the string.
Was This Post Helpful? 1
  • +
  • -

#3 jeremyjones123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-October 13

Re: can you post more than one string in a messageBox?

Posted 01 December 2013 - 05:31 PM

Ahhhh gotcha, knew it had to be an easy fix haha thanks brother

+rep
Was This Post Helpful? 0
  • +
  • -

#4 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 24-November 13

Re: can you post more than one string in a messageBox?

Posted 02 December 2013 - 01:18 PM

View Postjeremyjones123, on 01 December 2013 - 05:31 PM, said:

Ahhhh gotcha, knew it had to be an easy fix haha thanks brother

+rep


For string formatting there are other options for new lines as well.
\n is a new line and
\\ would actually create only one backslash. There are others to look into.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4479
  • View blog
  • Posts: 7,801
  • Joined: 08-June 10

Re: can you post more than one string in a messageBox?

Posted 02 December 2013 - 02:24 PM

I prefer not to rely on the '\n' escape character. Mainly because it's not necessarily portable, nor is it necessarily correct in Windows. Lastly because it's not as readable.

Windows expects line endings to be CR+LF. Carriage Return followed by Line Feed. '\n' is the Line Feed escape sequence. '\r' is the carriage return.

Now, in the vast, vast majority of scenarios, a LF by itself won't be a problem. Modern Windows can smooth it out for you. In fact, I don't think I've ever run into a situation where it didn't do what I wanted it to do. But, you know, correctness and all that.

As for portability, that probably doesn't matter to you either. It would only matter in very specific situations, but it doesn't hurt to write relatively portable code by default. But my point is that other OSs don't handle line endings the same way that Windows does. UNIX and other *NIX systems expect a lone LF, whereas Mac OS >=9 expected a lone CR. Using a built-in framework constant resolves that nicely.

Readability, now that's something that everyone should care about. I find it much easier to see Environment.NewLine when I'm scanning through code. It's also more expressive, something beginner developers would appreciate.

Sorry for the long rant on something so insignificant, but I just like discussing nuances.
Was This Post Helpful? 2
  • +
  • -

#6 vpdic  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 24-November 13

Re: can you post more than one string in a messageBox?

Posted 02 December 2013 - 04:18 PM

View PostCurtis Rutland, on 02 December 2013 - 02:24 PM, said:

Readability, now that's something that everyone should care about. I find it much easier to see Environment.NewLine when I'm scanning through code. It's also more expressive, something beginner developers would appreciate.


I agree entirely with everything you said. I was just pointing out the whole backslash dilemma that they will likely run into later.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1