How to add a line to a multiline TextBox?(如何在多行文本框中添加一行?)
问题描述
如何在 多行 文本框
?
例如伪代码;
textBox1.Clear();
textBox1.Lines.Add("1000+");
textBox1.Lines.Add("750-999");
textBox1.Lines.Add("400-749");
...snip...
textBox1.Lines.Add("40-59");
或
textBox1.Lines.Append("brown");
textBox1.Lines.Append("brwn");
textBox1.Lines.Append("brn");
textBox1.Lines.Append("brow");
textBox1.Lines.Append("br");
textBox1.Lines.Append("brw");
textBox1.Lines.Append("brwm");
textBox1.Lines.Append("bron");
textBox1.Lines.Append("bwn");
textBox1.Lines.Append("brnw");
textBox1.Lines.Append("bren");
textBox1.Lines.Append("broe");
textBox1.Lines.Append("bewn");
TextBox.Lines 实现(我可以看到)是:
The only methods that TextBox.Lines implements (that i can see) are:
- 克隆
- 复制到
- 等于
- 获取类型
- GetHashCode
- 获取枚举器
- 初始化
- GetLowerBound
- GetUpperBound
- 获取长度
- GetLongLength
- 获取值
- 设置值
- ToString
推荐答案
@Casperah 指出我想错了:
@Casperah pointed out that i'm thinking about it wrong:
TextBox
没有 行- 它有文字
- 如果需要,该文本可以在 CRLF 上拆分成行
- 但没有行 的概念
- A
TextBox
doesn't have lines - it has text
- that text can be split on the CRLF into lines, if requested
- but there is no notion of lines
那么问题是如何完成我想要的,而不是 WinForms 让我做什么.
The question then is how to accomplish what i want, rather than what WinForms lets me.
在其他给定的变体中存在细微的错误:
There are subtle bugs in the other given variants:
textBox1.AppendText("Hello" + Environment.NewLine);
textBox1.AppendText(Hello"+ ");
textBox1.Text += "Hello "
textbox1.Text += System.Environment.NewLine + "brown";
textBox1.AppendText("Hello" + Environment.NewLine);
textBox1.AppendText("Hello" + " ");
textBox1.Text += "Hello "
textbox1.Text += System.Environment.NewLine + "brown";
当(可能)不需要时,它们会附加或添加换行符.
They either append or prepend a newline when one (might) not be required.
所以,扩展助手:
public static class WinFormsExtensions
{
public static void AppendLine(this TextBox source, string value)
{
if (source.Text.Length==0)
source.Text = value;
else
source.AppendText("
"+value);
}
}
那么现在:
textBox1.Clear();
textBox1.AppendLine("red");
textBox1.AppendLine("green");
textBox1.AppendLine("blue");
和
textBox1.AppendLine(String.Format("Processing file {0}", filename));
注意:任何代码都会发布到公共领域.无需署名.
Note: Any code is released into the public domain. No attribution required.
这篇关于如何在多行文本框中添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在多行文本框中添加一行?


- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01