【问题标题】:C#: How to show a newline within a MessageBoxC#:如何在 MessageBox 中显示换行符
【发布时间】:2021-02-10 06:21:01
【问题描述】:

我经常使用MessageBox.Show("This is my message");显示消息。
有时我需要在较长的文本中换行。 通常我使用一种变体,每行放一个字符串:
MessageBox.Show("Line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3");
但我不喜欢“开销”。于是我找到了以下解决方案:
MessageBox.Show(string.Format("Line1{0}Line2{0}Line3", Environment.NewLine));
有没有更好的解决方案,开销更少?

【问题讨论】:

    标签: c# newline


    【解决方案1】:

    您几乎已经找到了两种方法。第三个是字符串插值(以避免“额外”string.Format 调用。

    $"Line1 {Environment.NewLine} Line2 {Environment.NewLine} Line3"
    

    提琴手示例:https://dotnetfiddle.net/16Wy57

    string.Format 方式几乎就是 at the official format docs 的示例

    我更喜欢插值,因为它避免了我的代码中的额外功能,但实际上这一切都归结为偏好。

    在任何选项中确实没有“开销”,因为操作很简单,而且在一天结束时,您可以连接字符串。

    【讨论】:

      【解决方案2】:

      如果您想要一个开销较小的可扩展解决方案,这里有一个干净且可扩展的解决方案:

      using System;
      
      var messageLines = new string[]
      {
          "Some line here",
          "More lines",
          "Could be loaded from a database",
          "With each row as a string"
      };
      
      MessageBox.Show(string.Join(Environment.NewLine, messageLines));
      

      【讨论】:

        【解决方案3】:

        MessageBox.Show("First Line\nSecond Line\nThird Line");
        \n 在适用于 MessageBox 的字符串中插入新行。
        在文件中注意这一点。您可以在那里找到\r\n\n\r\r\n - 这取决于操作系统或文件格式。

        【讨论】:

        • 这就是Environment.NewLine的原因,让您不必知道哪个是当前有效的。
        【解决方案4】:

        逐字字符串,只要确保字符串上没有标识即可。

        string v = 
        @"Hello, 
        ! Today is 
        , it's 
        now.";
        
        Console.WriteLine(v);
        

        输出:

        Hello, 
        ! Today is 
        , it's 
        now.
        

        演示:https://dotnetfiddle.net/WvjGfC

        【讨论】:

          【解决方案5】:

          如果您创建自己的 MessageBox 类,Luke Parker 的示例可以稍微完善一些(遗憾的是,它不能作为扩展方法完成,因为没有可用的 MessageBox 实例):

          namespace System.Windows.Forms
          {
              public static class MessageBoxLines
              {
                  public static void Show(string[] lines) 
                  {
                      MessageBox.Show(string.Join(Environment.NewLine, lines));
                  }
              }
          }
          

          你会像这样使用它:

          MessageBoxLines.Show(new[] { "Line1", "Line2"} );
          

          您可以为其他表单添加更多重载或可选参数(那些带有标题、指定按钮等的表单)


          如果您使用 params 数组,这可以进一步整理:

              public static void Show(params string[] lines) 
              {
                  MessageBox.Show(string.Join(Environment.NewLine, lines));
              }
          

          您会使用如下:

          MessageBoxLines.Show("Line1", "Line2" );
          

          但是使用 params 可能会干扰其他重载(按钮等)的标准模式,因此您必须通过重新排列顺序以使 params 排在最后,或者将所有 args进入 params 对象并将它们挖掘出来/将它们转换回它们应该是的(这有点小丑智能)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多