代码如下:            this.textBox2.Lines = new String[10];
            for (int i=0;i<10;i++)
            {
                this.textBox2.Lines[i]=Convert.ToString(i);
            }
这样的在textbox中发现产生了多个行,但是每行都是空的,没有数据.
但是如下却可以显示:            String[] strs = new String[10];
            
            for (int i=0;i<10;i++)
            {
                strs[i]=Convert.ToString(i);
            }
            this.textBox2.Lines = strs;为什么?请高手指点

解决方案 »

  1.   

    见帮助,在默认情况下,行的集合是 TextBox 中的行的只读副本。若要获取可写行集合,请使用与下面的代码相似的代码:textBox1.Lines = new string[] { "abcd" };
      

  2.   

    老兄:
    我在  System.Web.UI.WebControl.TextBox 和  System.Windows.Form.TextBox  中都没有看到  Lines 这个属性!!我很想知道你的 textBox2  到底是什么控件!!——换言之:你的这个控件可能内部代码 控制了不允许你 给子元素赋值!
      

  3.   

    代码如下: this.textBox2.Lines = new String[10];
      for (int i=0;i<10;i++)
      {
      this.textBox2.Lines[i]=Convert.ToString(i);
      }
    按时因为你没有个String复制啊 //this.textBox2.Lines是string[]
     this.textBox2.Lines = new String[3]{"abc","cde","fgh"};
     
      

  4.   

    public string[] Lines
    {
    get
    {
    string text = this.Text;
    ArrayList arrayList = new ArrayList();
    int j;
    for (int i = 0; i < text.Length; i = j)
    {
    for (j = i; j < text.Length; j++)
    {
    char c = text[j];
    if (c == '\r' || c == '\n')
    {
    break;
    }
    }
    string value = text.Substring(i, j - i);
    arrayList.Add(value);
    if (j < text.Length && text[j] == '\r')
    {
    j++;
    }
    if (j < text.Length && text[j] == '\n')
    {
    j++;
    }
    }
    if (text.Length > 0 && (text[text.Length - 1] == '\r' || text[text.Length - 1] == '\n'))
    {
    arrayList.Add("");
    }
    return (string[])arrayList.ToArray(typeof(string));
    }
    set
    {
    if (value != null && value.Length > 0)
    {
    StringBuilder stringBuilder = new StringBuilder(value[0]);
    for (int i = 1; i < value.Length; i++)
    {
    stringBuilder.Append("\r\n");
    stringBuilder.Append(value[i]);
    }
    this.Text = stringBuilder.ToString();
    return;
    }
    this.Text = "";
    }
    }原来这个属性是 TextBoxBase 的父类成员!
    ——这时微软的代码;意思很简单:
    你 Get 时:他将  Text  字符串 拆分开来作为新数组;
    你 Set 时:他将  数组  拼接成  Text;——你想对单个元素赋值:你用的是 Get 方法,获取的永远只是一个  字符串数组的 副本!!——你修改副本的值,根本不会对 Text 构成影响!!!
      

  5.   

    读完get set代码后基本明白了:
    1、类提供的是Lines属性的set get,而不是针对与某个数组元素的,通过Set代码可以看到,所以this.textBox2.Lines = new String[10];
    该行代码就会导致出现10个空行。因为String[10]的每个元素为空,而this.textBox2.Lines = new String[3]{"abc","cde","fgh"};则不会出现空行。
    2、再次提交例如:Lines[i]的时候,根据Get代码,实际上是在修改arrayList.ToArray这个返回的数组,
    因为这个是每次创建的(副本),所以说实际对象Text的值是不被修改的。另外补充:3、如果1、2分析的没错的话,想要修改已经存在的textBox中多行中某一行的话,应该把lines这个取出,然后修改某行,然后把Lines再赋回去。而不能对某一行Lines[I]修改。不知道理解是否正确???
    没问题的话,一会结帖了。