我如何在C#2010.NET控件数组? [英] How do I make a Control Array in C# 2010.NET?
问题描述
我最近搬到从Visual Basic 6到C#.NET 2010。
I recently moved from Visual Basic 6 to C# 2010 .NET.
在Visual Basic 6中有把你想有多少控件数组通过它改变了指数来使用的选项。
In Visual Basic 6 there was an option to put how many control arrays you would like to use by changing the "index" on it.
我想知道这是否可能在C#中,如果是这样我将如何去用它做一个类,如:
I am wondering if this is possible in C#, if so how would I go about doing it with a class like:
func fc = new func();
但随着不止一个数组中的FC,这可能吗?
But with more than just one array in fc, is this possible?
和进行更加明朗化,
的Visual Basic 6,当你加载像一个文本框或用户控制它在属性窗口索引a选项和控制,如果你更改为0,1,等等......它会允许你使用所有这些指标,而无需加载多个控件50倍。
Visual Basic 6 when you load a control like a text box or user control it has in the properties window a option for "Index" and if you change that to 0, 1, etc... it'll allow you to use all of those indexes, without loading multiple controls 50 times.
我想这可能是与一个ArrayList,但我不能完全肯定。
I think it might have something to do with an arraylist but I'm not entirely sure.
感谢您的帮助。
推荐答案
这code段是不会让你很远。创建控件数组是没有问题的,只是初始化它在窗体构造函数。然后,您可以将其作为一个属性,虽然这通常是一个糟糕的主意,因为你不希望暴露实现细节。事情是这样的:
That code snippet isn't going to get you very far. Creating a control array is no problem, just initialize it in the form constructor. You can then expose it as a property, although that's generally a bad idea since you don't want to expose implementation details. Something like this:
public partial class Form1 : Form {
private TextBox[] textBoxes;
public Form1() {
InitializeComponent();
textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
}
public ICollection<TextBox> TextBoxes {
get { return textBoxes; }
}
}
,然后让你写的:
Which then lets you write:
var form = new Form1();
form.TextBoxes[0].Text = "hello";
form.Show();
但是,不要让形式管理自己的文本框。
But don't, let the form manage its own text boxes.
这篇关于我如何在C#2010.NET控件数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!