如何在C#2010.NET中创建控件数组?

时间:2011-03-25 16:31:27

标签: c# arrays controls

我最近从Visual Basic 6迁移到C#2010 .NET。

在Visual Basic 6中,有一个选项可以通过更改它上面的“索引”来设置您想要使用多少个控件数组。

我想知道在C#中是否可以这样做,如果是这样的话,我将如何用类似的方式来做这件事:

func fc = new func();

但是fc中只有一个数组,这可能吗?

更明确一点,

Visual Basic 6当你加载像文本框或用户控件这样的控件时,它在属性窗口中有一个“索引”的选项,如果你将它改为0,1等等,它将允许你使用所有这些索引,而不加载50次多个控件。

我认为这可能与arraylist有关,但我不完全确定。

感谢您的帮助。

5 个答案:

答案 0 :(得分:9)

该代码段不会让你走得太远。创建一个控件数组没问题,只需在表单构造函数中初始化它。然后,您可以将其作为属性公开,尽管这通常是一个坏主意,因为您不希望公开实现细节。像这样:

public partial class Form1 : Form {
    private TextBox[] textBoxes;

    public Form1() {
        InitializeComponent();
        textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
    }

    public ICollection<TextBox> TextBoxes {
        get { return textBoxes; }
    }
}

然后让你写:

var form = new Form1();
form.TextBoxes[0].Text = "hello";
form.Show();

但不要,让表单管理自己的文本框。

答案 1 :(得分:3)

在.NET中,您将创建一个控件数组,然后您将为数组的每个元素实例化一个TextBox控件,设置控件的属性并将其放置在窗体上:

    TextBox[] txtArray = new TextBox[500];
    for (int i = 0; i < txtArray.length; i++)
    {
      // instance the control
      txtArray[i] = new TextBox();
      // set some initial properties
      txtArray[i].Name = "txt" + i.ToString();
      txtArray[i].Text = "";
      // add to form
      Form1.Controls.Add(txtArray[i]);
      txtArray[i].Parent = Form1;
      // set position and size
      txtArray[i].Location = new Point(50, 50);
      txtArray[i].Size = new Size(200, 25);
    }
.
.
.
Form1.txt1.text = "Hello World!";

除非您的布局更简单(即文本框的行和列),否则您可能会发现使用设计器更容易,更省时,更易于维护。

答案 2 :(得分:1)

与VB6不完全相同,但在c#中编写自己的代码非常容易。

如果您在设计器中创建一个控件,例如Button,则可以复制*.Designer.cs文件中的代码

通常看起来像这样

private System.Windows.Forms.Button button1;
...
this.button1.Location = new System.Drawing.Point(40, 294);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14;
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
...
this.Controls.Add(this.button1);

将代码剪切并粘贴到方法中,返回Button

private Button CreateButton()
{
    private System.Windows.Forms.Button button1;

    this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(75, 23);
    this.button1.TabIndex = 14; // <-- increase tab index or remove this line
    this.button1.Text = "Button1";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);

    this.Controls.Add(this.button1);
    return button;
}

然后像这样调用这个方法

List<Button> buttons = new List<Button>();
for(int i = 0; i < 10; i++)
{
    buttons.Add(CreateButton());
}

答案 3 :(得分:0)

答案 4 :(得分:0)

基于Trigo的模板:

这里有一个处理2维textBox数组的例子 panel1必须与设计师一起创建 (我有Autoscroll = true,大小= 858; 525)

public partial class Form1 : Form
{
    TextBox[,] txtBoxArray = new TextBox[2,100];
    public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < txtBoxArray.GetLength(0); i++)
        {
            for (int j = 0; j < txtBoxArray.GetLength(1); j++)
            {
                // instance the control
                txtBoxArray[i, j] = new TextBox();
                // set some initial properties
                txtBoxArray[i, j].Name = "txtBox_" + i.ToString() + "_" + j.ToString();
                txtBoxArray[i, j].Text = txtBoxArray[i, j].Name; //"";
                // add to form
                this.Controls.Add(txtBoxArray[i,j]);
                txtBoxArray[i, j].Parent = panel1;
                // set position and size
                txtBoxArray[i, j].Location = new Point(50+i*333, 50 + j * 25);
                txtBoxArray[i, j].Size = new Size(200, 25);
            }
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    //...


}

this will look like this

相关问题