如何在 C# 2010.NET 中创建控件数组? [英] How do I make a Control Array in C# 2010.NET?

查看:21
本文介绍了如何在 C# 2010.NET 中创建控件数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 当您加载像文本框或用户控件这样的控件时,它在属性窗口中有一个索引"选项,如果您将其更改为 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.

我认为这可能与数组列表有关,但我不完全确定.

I think it might have something to do with an arraylist but I'm not entirely sure.

感谢您的帮助.

推荐答案

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

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

然后让你写:

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屋!

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