简体   繁体   English

C#如何在Foreach循环中遍历表单控件

[英]How does C# iterate through Form Controls in a Foreach loop

As in the title, my question is how does C# iterate through form controls in a foreach loop? 就像标题中一样,我的问题是C#如何在foreach循环中遍历表单控件? In case that is a bit too vague I'll add a bit more detail: 如果有点太模糊,我将添加更多细节:

When I do foreach(Control c in MyForm) how does it index(?) through the controls? 当我执行foreach(Control c in MyForm) ,它如何通过控件进行索引(?)? Is it based on tab order or something else? 是基于制表符顺序还是其他? Any assistance would be appreciated as I'm trying to find out if I can place values into an array in a neater and more organized fashion. 在我试图找出是否可以将整齐,更有条理的方式将值放入数组时,我们将提供任何帮助。

foreach always works the same, so doing it over a form's controls doesn't change anything. foreach始终工作相同,因此在窗体的控件上进行操作不会更改任何内容。

It doesn't really use an index either; 它也不使用索引。 foreach only works on instances of classes that implement IEnumerable which means they have a GetEnumerator method. foreach仅适用于实现IEnumerable的类的实例,这意味着它们具有GetEnumerator方法。 foreach uses the returned IEnumerator to go through the collection one at a time; foreach使用返回的IEnumerator一次遍历一个集合; order is dependent on the actual implementation. 顺序取决于实际的实现。

For List (or any array) this is in-order (ie index 0 is first, 1 second, and so on); 对于List (或任何数组),这是有序的(即索引0是第一个,索引1是第二个,依此类推); for a random implementation of IEnumerable you would have to look at the source. 对于IEnumerable的随机实现,您必须查看源代码。 In your specific example, look at the type of the Controls collection on Form . 在您的特定示例中,查看FormControls集合的类型。 Its a ControlCollection so you'll want to look on Reference Source to find how it enumerates. 它是一个ControlCollection因此您将需要查看Reference Source以查找其枚举方式。

Based on the implementation it appears to be in-order with some safeguards against controls being removed during enumeration. 根据实施情况,它似乎是有序的,有一些保护措施可以防止在枚举期间删除控件。

It doesn't return controls based on tab index, it returns the controls in the order you added them the Controls collection. 它不基于选项卡索引返回控件,而是按将控件添加到Controls集合的顺序返回Controls

You can see this order in designer in Document Outline window. 您可以在设计器的“ 文档大纲”窗口中看到此顺序。 ( Ctrl + Alt + T ) and change the order by moving controls in tree in the Document Outline window. Ctrl + Alt + T ),然后通过在“ 文档大纲”窗口中的树中移动控件来更改顺序。 Also if you use Bring To front and Send To back toolbar buttons you can change this order in design time. 同样,如果使用“ Bring To front和“ Send To back工具栏按钮,则可以在设计时更改此顺序。

At run-time you can change this order by calling SendToBack and BringToFront method of controls or SetChildIndex method of Controls collection. 在运行时,可以通过调用控件的SendToBackBringToFront方法或Controls集合的SetChildIndex方法来更改此顺序。

To get the index of a control, you can use GetChildIndex method of Controls collection. 若要获取控件的索引,可以使用Controls集合的GetChildIndex方法。

Also keep in mind foreach(Control c in this.Controls) just will scan controls which you put directly on the form. 还请记住, foreach(Control c in this.Controls)只会扫描您直接放在窗体上的控件。 For example if you have a panel on form and the panel contains some controls, children of panel are in panel.Controls collection and not in controls collection of form. 例如,如果您在窗体上有一个面板,并且该面板包含一些控件,则该面板的子级位于panel.Controls集合中,而不在窗体的控件集合中。

Are you referring to the order that the controls are iterated over? 您是指控件的迭代顺序吗? I want to say that the controls are iterated over in the order that they were added to the Form's control collection. 我想说的是,这些控件是按照将它们添加到Form的控件集合中的顺序进行迭代的。 You can check this in the InitializeComponents designer method to see the order the controls are listed in that method. 您可以在InitializeComponents设计器方法中进行检查,以查看该方法中列出的控件的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM