如何在多行文本框中添加一行?

35

本文介绍了如何在多行文本框中添加一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 多行 文本框?

例如伪代码;

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.

这篇关于如何在多行文本框中添加一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14