Making an array of textboxes is not working

人走茶凉 提交于 2019-12-11 04:54:57

问题


Ok so here is my designer.cs code

for (int i = 0; i < textBoxes.Length; i++)
{
    textBoxes[i] = new System.Windows.Forms.TextBox();
    this.textBoxes[i].Location = new System.Drawing.Point(90, 50 + i * 20);
    this.textBoxes[i].Name = "textBox" + i;
    this.textBoxes[i].Size = new System.Drawing.Size(100, 20);
    this.textBoxes[i].TabIndex = i + 1;

    this.Controls.Add(textBoxes[i]);
}

This was edited code below the Windows Generated code

private System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[5];

I deleted any code that is related to any of my textboxes.

Instead it's giving me this error when I go to the design view:

Games MoreGames.Designer.cs Line:32 Column:1 

screenshot of error

The program can run but why wouldn't it let me access the designer so I can move things around?


回答1:


As Joel says, you should place that code in the constructor following the InitializeComponent() method in your MoreGames.cs file (NOT MoreGames.Designer.cs which cannot be edited) but you might also want to add the following:

textboxes[i].Parent = this;

That will tell each of your textboxes that the form is it's parent.

The basic namespace of your Form will look something like this:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[5];

        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < textBoxes.Length; i++)
            {
                textBoxes[i] = new System.Windows.Forms.TextBox();
                this.textBoxes[i].Location = new System.Drawing.Point(90, 50 + i * 20);
                this.textBoxes[i].Name = "textBox" + i;
                this.textBoxes[i].Size = new System.Drawing.Size(100, 20);
                this.textBoxes[i].TabIndex = i + 1;
                this.textBoxes[i].Parent = this;
                this.Controls.Add(textBoxes[i]);
            }
        }
    }
}



回答2:


You cannot edit the designer.cs file. Or rather, you can edit the file, but any changes you make will be overwritten every time the designer needs to regenerate the file, and certain things in the file will prevent the visual designer from rendering your form properly. This is by design.

What you should do instead is put your code in your form's constructor, immediately following the call to the InitializeComponent() method. The new controls won't show up on the screen for you to drag around, but they will be there when you actually run the program.

Moreover, it sounds like what you really need here is something that is more data driven, like a FlowLayoutPanel, that you can add and remove controls from at run time.



来源:https://stackoverflow.com/questions/20406481/making-an-array-of-textboxes-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!