Click here to Skip to main content
13,446,550 members (40,243 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have form contains textboxs all of it the same name with a number like(txt01,txt02,...)
when i make changes on these texts by using this code
foreach (Control txt in this.Controls)
                    if (txt is TextBox)

i get the texts but not in order by name or location
i want to know how it work? OR order the controls by what??
Posted 16-Aug-14 13:28pm
Wes Aday 16-Aug-14 19:39pm
The controls in the collection are not sorted. If you want to impose some sort of order on your controls then you could write a LINQ query and get the controls in an order you want.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It is a very basic, but somewhat confusing for the beginner loop. For might be familiar with the for loop,

for (int i = 0; i < 10; i++) {
   // writes the value of the i

.. this does the same, but has a different logic and syntax. Let me first make you understand the very basic of this loop's syntax.

foreach (datatype identifier in collection) {
   // do work here

datatype is the type of the data that we're going to work. In the above case it was int, identifier is the variable name that we're going to call the element in the list as, in the above case it was i. After that, in the above code there was a condition, in this we don't have a condition. Foreach loop creates a condition on itself, and we just have to worry about the elements. collection contains the list of the element. It can be an array, an enumeration or list etc. But must be able to contain elements.

I hope you're understanding the syntax, so now lets get to the code you're having.

foreach (Control txt in this.Controls) {
     if (txt is TextBox) {
        txt.Text = "something";

This was the code you were having, and you can see now, it is trying to access every object that is of Control type and then is naming it as txt. Where actually is it finding them? It is finding them inside the current context's Controls. Controls is some object inside the current class (being referenced by this and then calling the Controls object).

This loop would execute for each of the Control (txt is just a name that we're giving to it, it is not specified inside the class) and then it is checking the condition, that whether this Control is a TextBox (editable textarea) Control. If it is, then it is editting the value of it, to be "something".

Lets take one more look at the code now,

// foreach Control in current context's Controls, name it 'txt'
foreach (Control txt in this.Controls) {
     // check if it is a textbox
     if (txt is TextBox) {
        // if true, then edit its value and write "something in it"

I hope you got the point of the iteration. Now, the order thing? That is because the Collection you're working on is not ordered, try something like

Controls = Controls.OrderBy(r => r.Id)

This will order the controls by their ID (I wonder if they even have one; use your own values). This part must be went through first and then the iteration would come up to work with the ordered collection.

Good luck!
mdaban 16-Aug-14 20:06pm
Thank you for the perfect answer, but i cant find "OrderBy" in Controls
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Put the controls in an array (which implements IEnumerable) it will then access the controls in order. then loop through that array.

The ECMA-362 15.8.4 States

"The order in which foreach traverses the elements of an array, is as follows: For single-dimensional arrays elements are traversed in increasing index order, starting with index 0 and ending with index Length – 1. For multi-dimensional arrays, elements are traversed such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01-2016 | 2.8.180314.2 | Last Updated 16 Aug 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100