制作一组文本框不起作用 [英] Making an array of textboxes is not working

查看:52
本文介绍了制作一组文本框不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我的designer.cs代码

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]);
}

这是在 Windows 生成的代码下编辑的代码

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 

错误截图

程序可以运行,但为什么不让我访问设计器以便我可以移动东西?

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

推荐答案

正如 Joel 所说,您应该将该代码放在构造函数中,紧跟在您的 MoreGames.cs 文件中的 InitializeComponent() 方法之后(不是MoreGames.Designer.cs 无法编辑)但您可能还想添加以下内容:

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]);
            }
        }
    }
}

这篇关于制作一组文本框不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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