Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
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
C#
foreach (Control txt in this.Controls)
                    if (txt is TextBox)
                         txt.Text="something"

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
Comments
[no name] 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.

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."
 
Share this answer
 
It is a very basic, but somewhat confusing for the beginner loop. For might be familiar with the for loop,

C#
for (int i = 0; i < 10; i++) {
   // writes the value of the i
   Console.WriteLine(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.

C#
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.

C#
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,

C#
// 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"
        txt.Text="something";
     }
}


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

C#
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!
 
Share this answer
 
v2
Comments
M. Daban 16-Aug-14 20:06pm    
Thank you for the perfect answer, but i cant find "OrderBy" in Controls

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900