控制数组发生了什么 [英] What happened to control arrays

查看:133
本文介绍了控制数组发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我曾经使用 Visual Basic 6 编程,我能够创建具有相同名称的对象,然后通过索引区分它们。例如
,我们可以创建 TextBox1 和另一个 TextBox1 ,但使用不同的索引。
现在暂时无法使用此功能!
目前,我正在使用 Visual Studio 2012
无论如何操纵 VS2012 再次启用该功能,或者有类似的功能,因为它真的很有帮助。

Few years ago I used to program with Visual Basic 6, I was able to create objects with the same name, then differ them by the index. for example, we can create TextBox1 and another TextBox1 but with a different index. now this feature is not available anymore! Currently, I'm using Visual Studio 2012. Is there anyway to manipulate VS2012 to enable that feature again, or is there something similar to it, because it was really helpful.

推荐答案

今天完成类似事情的更简单方法是将所有这些控件放在一个公共父控件中。此父级可以是组框,面板,甚至是表单本身。

The easier way to accomplish a similar thing today is to place all of these controls in a common parent control. This parent could be a groupbox, a panel, or even the form itself.

因此,如果您的表单上的所有复选框都需要编入索引,没有例外,您不必执行任何特殊操作。如果只有一个复选框不同,则您需要该复选框以使其具有与索引复选框不同的父控件。在这种情况下,您可以在复选框组下面对面板控件进行分层,或者您可以在不同的单个复选框下对面板控件进行分层。两者都可以。

So if, say, all of the checkboxes on your form need to be indexed, with no exception, you don't have to do anything special. If just one checkbox is different, you need that checkbox to have a different parent control than your indexed checkboxes. In this case, you could layer a panel control under the checkbox group, or you can layer a panel control under the single checkbox that is different. Either will work.

稍后,您仍然无法通过索引访问这些复选框,但您可以将它们视为集合。以下是如何做到这一点:

Later on, you will still not be able to access these checkboxes by index, but you will be able to treat them as a collection. Here's how you can do that:

For Each box As CheckBox In Me.Controls.OfType(Of Checkbox)()
    'Do something with each checkbox
Next

或者如果您想知道哪些检查:

Or if you want to know which ones are checked:

Dim checkedBoxes As IEnumerable(Of Checkbox) = Me.Controls.OfType(Of Checkbox)().Where(Function(b) b.Checked)

如果你真的想要一个复选框数组,你可以使用这个技术得到一个。只需将这样的代码放入表单的加载事件中:

If you really want an array of the checkboxes, you can use this technique to get one. Just put code like this in your form's load event:

Dim checkBoxes() CheckBox = Me.Controls.OfType(Of CheckBox)().OrderBy(Function(b) b.Name).ToArray()

这篇关于控制数组发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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