如何将行添加到一个多行TextBox? [英] How to add a line to a multiline TextBox?

查看:491
本文介绍了如何将行添加到一个多行TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何一行文本添加到的多行中的 文本框



如伪代码;

  textBox1.Clear(); 
textBox1.Lines.Add(1000+);
textBox1.Lines.Add(750-999);
textBox1.Lines.Add(400-749);
...略...
textBox1.Lines.Add(40-59);

  textBox1.Lines.Append(棕色); 
textBox1.Lines.Append(brwn);
textBox1.Lines.Append(BRN);
textBox1.Lines.Append(眉);
textBox1.Lines.Append(BR);
textBox1.Lines.Append(BRW);
textBox1.Lines.Append(brwm);
textBox1.Lines.Append(布龙);
textBox1.Lines.Append(BWN);
textBox1.Lines.Append(brnw);
textBox1.Lines.Append(BREN);
textBox1.Lines.Append(broe);
textBox1.Lines.Append(bewn);

这的 TextBox.Lines 工具(即我可以看到)是:




  • 克隆

  • CopyTo从

  • 等于

  • 的GetType

  • 的GetHashCode

  • 的GetEnumerator

  • 初始化

  • GetLowerBound

  • GetUpperBound

  • 对GetLength

  • GetLongLength

  • 的GetValue

  • 的SetValue

  • 的ToString




解决方案

@Casperah指出,我在想是错误的。 A 文本框没有的有无的线,它具有的文本。文本可以在CRLF分割成线,如果要求 - 但没有线的概念



接下来的问题是如何实现我想要什么,而不是什么样的WinForms让我。



其他特定变种有个微妙的问题:




  • textBox1.AppendText(你好+ Environment.NewLine);

  • textBox1.AppendText(你好+\r \\\
    );

  • textBox1.Text + =Hello\r\\\

  • textbox1.Text + = System.Environment.NewLine +棕色;



  • 他们要么追加或预先时(可能)不要求一个新行



    所以,扩展帮助:

     公共静态类WinFormsExtensions 
    {
    公共静态无效AppendLine(该文本框源,字符串值)
    {
    如果(source.Text.Length == 0)
    source.Text =价值;
    ,否则
    source.AppendText(\r\\\
    +值);
    }
    }



    所以现在:

      textBox1.Clear(); 
    textBox1.AppendLine(红);
    textBox1.AppendLine(绿色);
    textBox1.AppendLine(蓝);

      textBox1.AppendLine(的String.Format(处理文件{0},文件名)); 




    注意:任何代码被释放到公共领域。没有归属的需要。



    How can i add a line of text to a multi-line TextBox?

    e.g. pseudocode;

    textBox1.Clear();
    textBox1.Lines.Add("1000+");
    textBox1.Lines.Add("750-999");
    textBox1.Lines.Add("400-749");
    ...snip...
    textBox1.Lines.Add("40-59");
    

    or

    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");
    

    The only methods that TextBox.Lines implements (that i can see) are:

    • Clone
    • CopyTo
    • Equals
    • GetType
    • GetHashCode
    • GetEnumerator
    • Initialize
    • GetLowerBound
    • GetUpperBound
    • GetLength
    • GetLongLength
    • GetValue
    • SetValue
    • ToString

    解决方案

    @Casperah pointed out that i'm thinking about it wrong. 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.

    The question then is how to accomplish what i want, rather than what WinForms lets me.

    Other given variants have a subtle bug:

    • textBox1.AppendText("Hello" + Environment.NewLine);
    • textBox1.AppendText("Hello" + "\r\n");
    • textBox1.Text += "Hello\r\n"
    • textbox1.Text += System.Environment.NewLine + "brown";

    They either append or prepend a newline when one (might) not be required.

    So, extension helper:

    public static class WinFormsExtensions
    {
       public static void AppendLine(this TextBox source, string value)
       {
          if (source.Text.Length==0)
             source.Text = value;
          else
             source.AppendText("\r\n"+value);
       }
    }
    

    So now:

    textBox1.Clear();
    textBox1.AppendLine("red");
    textBox1.AppendLine("green");
    textBox1.AppendLine("blue");
    

    and

    textBox1.AppendLine(String.Format("Processing file {0}", filename));
    

    Note: Any code is released into the public domain. No attribution required.

    这篇关于如何将行添加到一个多行TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆