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
TextBoxdoesn'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.
这篇关于如何在多行文本框中添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在多行文本框中添加一行?
基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
