窗体上枚举控件 [英] Enumerating Controls on a Form
问题描述
我有一个就可以了约30控件的窗体,当用户点击一个按钮,从每个控制数据被保存到一个文件中。但是我需要去通过表单(这是我能做到)在每一个控制,但它需要是为了,例如,我需要开始就在表格的左上方和下工作,我的路到底正确的形式。
I have a form with about 30 controls on it, and when the user clicks a button, data from each control is saved to a file. But I need to go through every single control on the form (which I can do), but it needs to be in order, For example, I need to start right at the top left of the form and work my way down to the bottom right of the form.
这是一个foreach循环将如何运作?即:
Is this how a foreach loop would work? I.e.:
foreach(Control control in this.Controls)
{
}
或者它不经过他们为了?
Or does it not go through them in order?
推荐答案
这将返回控制在它们被添加到形式,而不是在可视顺序的顺序。
That would return the controls in the order they are added to the form, not in visual order.
如果您已经设置了的TabIndex
作为要分析你可以使用LINQ通过的TabIndex <对它们进行排序的控件相同的顺序/ code>。
If you have set up your TabIndex
in the same order as you want to parse the controls you can use LINQ to sort them by TabIndex
.
foreach (Control control in this.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
{
}
这篇关于窗体上枚举控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!