0

您好,我有以下代码-

        richTextBox1.Text = richTextBox1.Text + action + "ok: " + ok.ToString();
        richTextBox1.Text = richTextBox1.Text + "err: " + err.ToString();
        richTextBox1.Text = richTextBox1.Text + "\r\n";
        textBox1.Text = textBox1.Text;

结果看起来像 -

好的:7错误:0

但我想要-

好的:7

错误:0

有了间距,为了让它看起来更好,我该怎么做?

4

3 回答 3

3

您可以添加另外 2 行:

richTextBox1.Text += Environment.NewLine;
richTextBox1.Text += Environment.NewLine;

在你的“ok”和“err”之间——假设你想要在两行输出之间有一个空行。但是,您应该使用string.Format或 aStringBuilder将输出创建为连接字符串,这种方式效率低下。

你也不需要决赛:

textBox1.Text = textBox1.Text;

因为这只是将文本框内容设置回自身并且什么都不做。

于 2011-04-29T12:40:12.753 回答
1

你已经得到你的答案,你只是把它放在错误的地方!关键是使用转义序列\r\n,它插入一个回车符和一个新行。

此外,没有理由将此代码分成多行。这样做最终会导致性能损失。最好一次完成所有字符串连接。(您在这里没有做足够的连接来证明使用StringBuilder该类的合理性,但值得记住的是字符串在 .NET 中是不可变的并相应地编写代码。)

尝试像这样重写代码:

textBox1.Text = textBox1.Text + action + "ok: " + ok.ToString(); + "\r\n" +
                "err: " + err.ToString(); + "\r\n";

您还可以完全消除最后一行代码,因为它只是将值设置textBox1.Text为自身。这是一个无操作,这意味着它什么都不做。

于 2011-04-29T12:40:42.130 回答
1

first that you could do all these in a single statement, second you could use += operator instead, and third what is that last statement doing?! it not needed, fourth add "\n" after each part you need there is no limit where you should put it, no "\r" needed.

于 2011-04-29T12:44:35.263 回答