accessing objects in form by index or somthing like index
-
as you said, tag is more proper here. isn't it any way more optimized than those tow loops? where they correct or not at last? how can i make it optimized (with consideration that i use tags instead of EndsWith) thanks again
I think the
Tag
solution with just theforeach
loop is the correct way to do this. As you said yourself, your solution will add some controls twice or some none at all (depending whether youcontinue
out of the loop or not), becauseEndsWith("1")
would trigger for both pictureBox1 and pictureBox11, depending on the order in which the picture boxes are returned in theforeach
loop. regards -
I think the
Tag
solution with just theforeach
loop is the correct way to do this. As you said yourself, your solution will add some controls twice or some none at all (depending whether youcontinue
out of the loop or not), becauseEndsWith("1")
would trigger for both pictureBox1 and pictureBox11, depending on the order in which the picture boxes are returned in theforeach
loop. regardsexcept the Tag solution, do you offer anything else? why just 'foreach'? you mean without the extern 'for'? i can't find out why. i wonder if you explain more about the reason of ommiting the extern 'for'. thanks again and again :)
-
except the Tag solution, do you offer anything else? why just 'foreach'? you mean without the extern 'for'? i can't find out why. i wonder if you explain more about the reason of ommiting the extern 'for'. thanks again and again :)
Sajjad Izadi wrote:
except the Tag solution, do you offer anything else?
I'd say it's the best solution for your problem.
Sajjad Izadi wrote:
why just 'foreach'? you mean without the extern 'for'? i can't find out why.
Because using the Tag, the foreach loop will already find all the controls you are looking for.
foreach (Control cont in this.Controls) { if (cont is PictureBox) { pic = cont as PictureBox; if(pic != null) { string tag = pic.Tag as string; int number = int.Parse(tag); // this might fail, add a check here if (tag != null && number >= 1 && number <= 16) Console.WriteLine(number); } } }
-
Sajjad Izadi wrote:
except the Tag solution, do you offer anything else?
I'd say it's the best solution for your problem.
Sajjad Izadi wrote:
why just 'foreach'? you mean without the extern 'for'? i can't find out why.
Because using the Tag, the foreach loop will already find all the controls you are looking for.
foreach (Control cont in this.Controls) { if (cont is PictureBox) { pic = cont as PictureBox; if(pic != null) { string tag = pic.Tag as string; int number = int.Parse(tag); // this might fail, add a check here if (tag != null && number >= 1 && number <= 16) Console.WriteLine(number); } } }
but i want the pictures in order of their position at form. does it response for the order? and a non related question: what is that 'int?' ?
-
but i want the pictures in order of their position at form. does it response for the order? and a non related question: what is that 'int?' ?
-
It's a Nullable Type[^]. But I modified the sample a bit, because internally Control will cast the tag value to a string, so the int solution will not work here. See my modified post. regards
what if i want the in order of their position in screen? should i ommit the outer 'for' still?
-
what if i want the in order of their position in screen? should i ommit the outer 'for' still?
-
Depends on how you define "position". The tag value, the (x,y) position, the order of how they were added to the form?
how is it if i want them in (x,y) position? how is it if i want to add them in order of tag value? :) and what if i want them in order of they were added to form? :-D you will help me if multiple aspects if you answer these threes. thanks in advanced ;)
-
how is it if i want them in (x,y) position? how is it if i want to add them in order of tag value? :) and what if i want them in order of they were added to form? :-D you will help me if multiple aspects if you answer these threes. thanks in advanced ;)
-
ok man ..., you're right :-D: i have to write them myself. thanks for your really hale answers :)