创建动态控件 [英] Creating dynamic controls

查看:66
本文介绍了创建动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        int i = amount; //amount will always start at 0
        int j = i + 1;

        GroupBox[] verGroup;
        verGroup = new GroupBox[i];

        verGroup[i].Name = "verGroup" + i.ToString();
        verGroup[i].Width = 400;
        verGroup[i].Height = 120;
        verGroup[i].Left = 5;
        verGroup[i].Top = 5 + (verGroup[i].Height * i) + (10 * i);
        verGroup[i].Text = "Verification #" + j.ToString();

        pnlVer.Controls.Add(verGroup[i]);

它在verGroup [i] .Name处给我一个IndexOutofRangeException。但是索引是0,这肯定是它想要的吗?

It gives me an IndexOutofRangeException at verGroup[i].Name. But index is 0, which is surely what it wants?

我也尝试过

 verGroup = new GroupBox[5]  

但这会引发未将对象引用设置为对象实例错误。

but that throws an "Object reference not set to an instance of an object" error.

如果有人能指出我正确的方向,将不胜感激。

Would be appreciated if someone could point me in the right direction.

推荐答案

由于金额从0开始,并且您创建了一个大小为i的数组,因此您正在创建一个大小为0的数组。您不能为数组中的任何内容建立索引,因为它的长度为0。

Since amount starts at 0, and you create an array of size i, you are creating an array of size 0. Therefore you can't index anything in the array, because it is of length 0.

第二个错误是因为您没有初始化组框。您需要说verGroup [i] = new GroupBox();初始化它。

the second error is because you don't initialize the group box. You need to say verGroup[i] = new GroupBox(); to initialize it.

这篇关于创建动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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