为文本框结果添加间距
您好,我有以下代码 -
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;
结果看起来像 -
ok:7err:0
但我想要
- ok:7
err:0
带间距,使其看起来更好我该怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以添加另外 2 行:
在“ok”和“err”之间 - 假设您希望在两行输出之间有一个空行。但是,您应该使用 string.Format 或 StringBuilder 将输出创建为连接字符串,这种方式效率低下。
您也不需要最后一个:
因为这只是将文本框内容设置回其自身,并且不执行任何操作。
You could add another 2 lines:
between your "ok" and "err" - assuming you want a blank line between the two lines of output. However, you should either be using
string.Format
or aStringBuilder
to create your output as concatenating strings this way in inefficient.You also don't need the final:
as that is just setting the text box contents back to itself and does nothing.
你已经得到答案了,只是放错地方了!关键是使用转义序列
\r\n
,它插入一个回车符和一个新行。另外,没有理由将此代码分成多行。这样做最终会导致性能损失。最好一次性完成所有字符串连接。 (这里没有进行足够的串联来证明使用 StringBuilder 类的合理性,但值得记住的是,字符串在 .NET 中是不可变的,并相应地编写代码。)
尝试重写代码,如下所示
:还可以完全消除最后一行代码,因为只需将
textBox1.Text
的值设置为其自身。这是一个无操作,意味着它什么也不做。You've already got your answer, you just have it in the wrong place! The key is to use the escape sequence
\r\n
, which inserts a carriage return and a new line.Also, there's no reason to split this code up into multiple lines. You end up incurring a performance penalty for doing so. It's better to do all of the string concatenation at one time. (You aren't doing enough concatenations here to justify using the
StringBuilder
class, but it's worth keeping in mind that strings are immutable in .NET and writing code accordingly.)Try rewriting the code like this:
You can also complete eliminate the last line of code, as that simply sets the value of
textBox1.Text
to itself. It's a no-op, meaning that it does nothing at all.首先,您可以在一条语句中完成所有这些操作;其次,您可以使用 += 运算符;第三,最后一条语句在做什么?不需要,第四个在您需要的每个部分后添加“\n”,放置位置没有限制,不需要“\r”。
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.