gpt4 book ai didi

c# - 不同行的下一个文本输入

转载 作者:太空狗 更新时间:2023-10-29 22:35:46 26 4
gpt4 key购买 nike

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

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);
}

如何定义每个新输入在不同的行上?因为现在所有错误都在 1 长行中。

14:15:00 Error 1 14:16:00 Error 2 14:17:00 Error 3

而不是像这个例子那样在每个错误之间使用换行符来读取:

14:15:00 Error 1
14:16:00 Error 2
14:17:00 Error 3

最佳答案

添加一个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);

会更快。下面是一个 1000 行的 LINQPad 基准代码:

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);

}

输出:

+ concatentation took (on my machine) 16 msek
Concat needed 10 msek

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

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

关于c# - 不同行的下一个文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48342150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com