12 Replies - 7962 Views - Last Post: 02 November 2012 - 01:48 PM Rate Topic: -----

#1 SexySquirrel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 30-August 09

Multi lined Message Box

Post icon  Posted 23 December 2009 - 08:47 PM

How would I create a multi lined message box?
Here's what I've got so far:

MessageBox.Show("Line #1" "Line #2", "Multi-Lined MessageBox", MessageBoxButtons.OK, MessageBoxIcon.Information);

Is This A Good Question/Topic? 0
  • +

Replies To: Multi lined Message Box

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Multi lined Message Box

Posted 23 December 2009 - 10:14 PM

How about:
MessageBox.Show("Line #1" + Environment.NewLine + "Line #2");
?
Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Multi lined Message Box

Posted 23 December 2009 - 10:50 PM

You can also try using \n characters so you dont' have to have that unnecessary white space.
MessageBox.Show("Line #1\nLine#2", "Multi-Lined MessageBox", MessageBoxButtons.OK, MessageBoxIcon.Information);



\n represents a new line
\t represents a tab
\" represents a double quote, use this when wanting to quote something inside of a string. MessageBox.Show("Someone once said, \"Hello World!\"");

Hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Multi lined Message Box

Posted 24 December 2009 - 01:13 AM

Although the \n or \r\n makes less code to write, but using Environment.NewLine is I think a better approach, since this creates platform independent new line... Something like that as I can remember :-)
Was This Post Helpful? 0
  • +
  • -

#5 SexySquirrel  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 97
  • Joined: 30-August 09

Re: Multi lined Message Box

Posted 24 December 2009 - 02:17 PM

View PostFlashM, on 24 Dec, 2009 - 12:13 AM, said:

Although the \n or \r\n makes less code to write, but using Environment.NewLine is I think a better approach, since this creates platform independent new line... Something like that as I can remember :-)

The "/n" works just fine for what I want it and is less code, thanks :)
Was This Post Helpful? 0
  • +
  • -

#6 lklauder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 08

Re: Multi lined Message Box

Posted 29 October 2012 - 12:45 PM

Here is one way to display a multi-line message:

         StringBuilder format_Builder = new StringBuilder(" {0} "); 

         for (int i_line = 1; i_line < 28; i_line++)
         {
            format_Builder.Append( "\n {" + i_line.ToString() + "} " );
         }

         str_msg = string.Format
            ( format_Builder.ToString()
            , "The k_line program group supports three different types of railroad track "
            , "geometry calculation as follows: "
            , "1) generation of coordinate data for a transition that goes through the "
            , "minimum  number of points needed to define the geometry and that does not "
            , "involve 'fitting';"
            , "2) fitting relative geometry to track shapes defined by measured chord offsets "
            , "such as those typically measured by computer controlled tampers; and"
            , "3) fitting absolute geometry to track centerline coordinates measured by some "
            , "type of geographic survey. "
            , "This dialog is relevant only for absolute geographic results from the 3rd type of "
            , "fitting of geometry to railroad track centerline data. The role of this dialog is "
            , "to prepare two 'comma separated value' files of data characterizing fitted track "
            , "geometry for import into a track information database."
            , ""
            , "When geometry is fitted to a section of track the results are less reliable for "
            , "the first and last curves of the track section than for curves that are bounded "
            , "on each side by a sequence such as tangent-spiral-curve-spiral-tangent or by "
            , "reverse_-spiral-curve-spiral-curve, etc. Therefore it is desirable when fitting"
            , "geometry to absolute measured track centerline data to get fits based on "
            , ".align_run_def files that encompass three or more curves and and to export info "
            , "from a given run only for curves that are interior to bounding sequences like the "
            , "ones just described. Thus it is recomeneded that a user look at the graphic "
            , "display of the current solution's results, identify one or more interior curves "
            , "for export, and on this dialog select each one in turn in the list of included "
            , "curves on this dialog and click the export button to export its info. When the "
            , "export button is clicked the info for the selected curve is copied from files of "
            , "info for all curves of the current solution and appended to files suitable for "
            , "import into track geometry info database tables. "
            );
         MessageBox.Show(str_msg, "Multi-Lined MessageBox"); 


Was This Post Helpful? 0
  • +
  • -

#7 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: Multi lined Message Box

Posted 30 October 2012 - 06:28 AM

@OP

Don't use the "\n" newline notation. Instead, use Environment.NewLine as it is stated above.

Because it's better to learn something the good way at the beginning, then to correct it later.

On UNIX platforms Environment.NewLine is equal to "\n" and on non-unix platforms it's equal to "\r\n".

So use that environment variable, it'll make your code platform independent. It's maybe not that important in this case, but it's bound to come in handy later.

http://msdn.microsof...(v=vs.100).aspx

This post has been edited by RexGrammer: 30 October 2012 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 lklauder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 08

Re: Multi lined Message Box

Posted 30 October 2012 - 08:19 AM

Good comment; I had not been thinking about that aspect. It prompted me to look a little further, and I came up with something I have been wanting for a while as illustrated in the C# code below (to which additional MessageBox.Show optional arguments can easily be added).

Since the following does not handle variable info it would be even nicer if the .Net folks would add to the string.Format statement the ability to look at what you put in as arguments and if there is no explicit argument substitution formatting then automatically construct a default format string incorporating line breaks where they are present in the source code for the arguments of the string.Format statement.


      void button_explain_Click(object sender, EventArgs e)
      {
         string[] text = new string[]
         { "The k_line program group supports three different types of railroad track "
         , "geometry calculation as follows: "
         , ""
         , "1) generation of coordinate data for a transition that goes through the "
         , "minimum  number of points needed to define the geometry and that does not "
         , "involve 'fitting';"
         , ""
         , "2) fitting relative geometry to track shapes defined by measured chord offsets "
         , "such as those typically measured by computer controlled tampers; and"
         , ""
         , "3) fitting absolute geometry to track centerline coordinates measured by some "
         , "type of geographic survey. "
         , ""
         , "This dialog is relevant only for absolute geographic results from the 3rd type of "
         , "fitting of geometry to railroad track centerline data. The role of this dialog is "
         , "to prepare two 'comma separated value' files of data characterizing fitted track "
         , "geometry for import into a track information database."
         , ""
         , "When geometry is fitted to a section of track the results are less reliable for "
         , "the first and last curves of the track section than for curves that are bounded "
         , "on each side by a sequence such as tangent-spiral-curve-spiral-tangent or "
         , "reverse_-spiral-curve-spiral-curve, etc. Therefore it is desirable when fitting"
         , "geometry to absolute measured track centerline data to get fits based on "
         , ".align_run_def files that encompass three or more curves and and to export info "
         , "from a given run only for curves that are interior to bounding sequences like the "
         , "ones just described. Thus it is recommeneded that a user look at the graphic "
         , "display of the current solution's results, identify one or more interior curves "
         , "for export, and on this dialog select each one in turn in the list of included "
         , "curves on this dialog and click the export button to export its info. When the "
         , "export button is clicked the info for the selected curve is copied from files of "
         , "info for all curves of the current solution and appended to files suitable for "
         , "import into track geometry info database tables. "
         };

         show_in_MessageBox_the_lines_of(text);
      }

      void show_in_MessageBox_the_lines_of(string[] text)
      {
         StringBuilder msg_Builder = new StringBuilder(" " + text[0] + " ");
         for (int i_line = 1; i_line < text.Length; i_line++)
         {
            msg_Builder.Append(Environment.NewLine + " " + text[i_line] + " ");
         }
         MessageBox.Show(msg_Builder.ToString(), "Multi-Lined MessageBox"); 
      }



Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Multi lined Message Box

Posted 02 November 2012 - 10:56 AM

Ouch! You are using the StringBuilder class, but you are still also using the + operator.

I recommend something like this instead:
void ShowInMessageBoxAllLines(IEnumerable<string> lines)
{
    var sb = new StringBuilder();
    bool follower = false;
    foreach(string line in lines)
    {
        if (follower)
            sb.Append(Environment.NewLine);
        sb.Append(' ');
        sb.Append(line);
        sb.Append(' ');
        follower = true;
    }
    MessageBox.Show(sb.ToString(), "Multi-Lined Message Box");
}


Was This Post Helpful? 0
  • +
  • -

#10 lklauder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-December 08

Re: Multi lined Message Box

Posted 02 November 2012 - 11:09 AM

nice improvement.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Multi lined Message Box

Posted 02 November 2012 - 12:00 PM

There is also sb.AppendLine which automatically adds a Environment.Newline after the content.

This post has been edited by AdamSpeight2008: 02 November 2012 - 12:09 PM

Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Multi lined Message Box

Posted 02 November 2012 - 12:03 PM

Assuming that String.Join() is efficient, you can also go for this:
void ShowInMessageBoxAllLines(IEnumerable<string> lines)
{
    string body = String.Join(" " + Environment.NewLine + " ", lines);
    MessageBox.Show(String.Format(" {0} ", body), "Multi-Lined Message Box");
}


Was This Post Helpful? 0
  • +
  • -

#13 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: Multi lined Message Box

Posted 02 November 2012 - 01:48 PM

Look at the posts #5 and #6(date difference), i though that you don't like to resurrect dead threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1