代码之家  ›  专栏  ›  技术社区  ›  Belekz

在不同行上输入下一个文本

  •  5
  • Belekz  · 技术社区  · 7 年前

    我有用于诊断目的的文本框。背后的代码非常简单:

    XAML:

    <TextBox HorizontalAlignment="Left" Margin="640,20,0,0" TextWrapping="Wrap" Height="280" Width="840" Name="txtDiagnostic" IsHitTestVisible="True" />
    

    C#:

    private void AddMessage(string message)
    {
        txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message);
    }
    

    如何定义每个新输入位于不同的行上?因为现在所有的错误都在一条长线上。

    14: 15:00错误1 14:16:00错误2 14:17:00错误3

    而不是在每个错误之间使用换行符进行可读,如以下示例所示:

    14: 15:00错误1
    14: 16:00错误2
    14: 17:00错误3

    3 回复  |  直到 5 年前
        1
  •  5
  •   Mong Zhu Bart de Boer    7 年前

    添加 Environment.NewLine 在每个字符串的末尾

    txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message) + Environment.NewLine;
    

    并确保文本框 capable of multiline

    XAML:

    <TextBox
      Name="tbMultiLine"
      TextWrapping="Wrap"
      AcceptsReturn="True"                    <-- IMPORTANT
      VerticalScrollBarVisibility="Visible"   <-- IMPORTANT
    >
    

    编辑:作为对常规的响应 string concatination debate 你当然可以使用 string.Concat()

    String.Concat(txtDiagnostic.Text,DateTime.Now.ToString("hh:mm:ss:fff") , " " , "ERROR....." , Environment.NewLine);
    

    它会更快。以下是LINQPad的基准代码,共1000行:

    void Main()
    {
        Stopwatch sw = new Stopwatch();
    
        string text = "";
        sw.Start();
        for (int i = 0; i < 1000; i++)
        {
            //text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + "ERROR.....") + Environment.NewLine;
            String.Concat(text,DateTime.Now.ToString("hh:mm:ss:fff") , " " , "ERROR....." , Environment.NewLine);
        }
        sw.Stop();
        Console.WriteLine("ELAPSED: " + sw.ElapsedMilliseconds);
    
    }
    

    输出:

    + 浓缩时间(在我的机器上)16毫秒
    Concat 需要10毫秒

    选择您自己,您应该知道要“通知”用户多少错误消息;)

    免责声明:1000行是一个非常糟糕的基准,但我在这里选择它是为了适合手头的用例。阅读超过1000行(甚至1000行)的错误消息并不是我想使用的软件。如果您开始连接较大的行集(x>1000),那么您确实应该使用 StringBuilder 正如在 字符串串联辩论 我提供的链接。

        2
  •  2
  •   L. Guthardt    6 年前

    实施 Environment.NewLine 从源代码:

    中的实现。净额4.6.1: Source

    /*===================================NewLine====================================
    **Action: A property which returns the appropriate newline string for the given
    **        platform.
    **Returns: \r\n on Win32.
    **Arguments: None.
    **Exceptions: None.
    ==============================================================================*/
    public static String NewLine {
        get {
            Contract.Ensures(Contract.Result<String>() != null);
            return "\r\n";
        }
    }
    

    所以你可以一起去 \r\n 作为 string 作为输出文本,结果与Mong Zhu的答案完全相同,因为 环境换行符 是它的实现。

    txtDiagnostic.Text += (DateTime.Now.ToString("hh:mm:ss:fff") + " " + message + "\r\n");
    

    如果您使用 \n \r\n . 在Windows上,它实际上是 \r\n .

    来自MSDN:

    包含“\r\n”的字符串 非Unix平台,或字符串 包含Unix平台的“\n”。

        3
  •  1
  •   Luke    7 年前

    还可以使用AppendText():

    this.txtTest.AppendText("blablabla" + Environment.NewLine);