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

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

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

我想知道这是否可能在C#中,如果是这样的话,我将如何使用类来执行此操作:

func fc = new func(); 

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

更加明确,

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

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

谢谢你的帮助。

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

 public partial class Form1 : Form { private TextBox[] textBoxes; public Form1() { InitializeComponent(); textBoxes = new TextBox[] { textBox1, textBox2, textBox3 }; } public ICollection TextBoxes { get { return textBoxes; } } } 

然后让你写:

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

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

在.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!"; 

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

与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 

阅读MS的这一过渡指南: http : //msdn.microsoft.com/en-us/library/kxt4418a%28v=vs.80%29.aspx

基于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) { } //... } 

这将是这样的