0

我正在开发一个桌面应用程序,我对这段代码感到震惊,我想在彼此的正下方创建 15 个富文本框。但它一直给我一个“对象引用未设置为...”的错误。

        RichTextBox[] richboxes = new RichTextBox[14];
        Array rboxarray = Array.CreateInstance(typeof(RichTextBox),14);
        int y = 128;
        int j = 0;
        foreach(RichTextBox rbox in rboxarray)
        {
            Controls.Add(rbox);
            rbox.Location = new System.Drawing.Point(14, y);
            rbox.Name = "richTextBox"+ (12+j);
            rbox.Size = new System.Drawing.Size(671, 68);
            rbox.TabIndex = 41 + j;
            rbox.Text = "";
            y += 70;
            j++;
        }

但是出错就行了rbox.Location = new System.Drawing.Point(14, y);。请给我正确的代码。

4

2 回答 2

4

在 RichTextBoy-Array-Instance 使用 For-Loop 它将起作用:

        for (int i = 0; i < richboxes.Length; i++)
        {
            richboxes[i] = new RichTextBox(); // Instance the TextBox

            Controls.Add(richboxes[i]);
            richboxes[i].Location = new System.Drawing.Point(14, y);
            richboxes[i].Name = "richTextBox" + (12 + j);
            richboxes[i].Size = new System.Drawing.Size(671, 68);
            richboxes[i].TabIndex = 41 + j;
            richboxes[i].Text = "";
            y += 70;
            j++;
        }
于 2013-06-27T06:21:03.227 回答
0
      RichTextBox[] txt = new RichTextBox[15];

        for (int i = 0; i < 15; i++)
        {
            txt[i] = new RichTextBox();
            txt[i].Text = "";
            if (i > 0)
            {
                txt[i].Left = txt[i - 1].Right;
            }

            this.Controls.Add(txt[i]);
        }
于 2013-06-27T06:40:19.627 回答