|
-
Jan 22nd, 2008, 02:15 PM
#1
Thread Starter
Lively Member
[RESOLVED] [3.0/LINQ] Foreach (Picturebox pbx...?
Hello!
I have 9 pictureboxes in a window's form.
I wish to set the image of all of these to one that I specify.
How can I do this with a foreach-loop, or is there any other way?
I tried to use:
Code:
foreach (PictureBox pbx in this)
{
pbx.Image = imgBoard;
}
but it doesn't work.
Thanks for any help at all!
Cheers,
Zolomon
-
Jan 22nd, 2008, 02:17 PM
#2
Addicted Member
Re: [3.0/LINQ] Foreach (Picturebox pbx...?
you loop through the control container not the form itself
-
Jan 22nd, 2008, 02:21 PM
#3
Thread Starter
Lively Member
Re: [3.0/LINQ] Foreach (Picturebox pbx...?
Thanks for a quick answer!
Just found the control container, and I get no compile error but the application crashes just as it starts.
Any thoughts?
Here is my source:
http://monoport.com/6633
Cheers,
Zolomon
-
Jan 22nd, 2008, 02:26 PM
#4
Addicted Member
Re: [3.0/LINQ] Foreach (Picturebox pbx...?
Code:
foreach (Control c in this.Controls)
{
if (c is PictureBox)
{
// do code
}
}
-
Jan 22nd, 2008, 02:31 PM
#5
Re: [3.0/LINQ] Foreach (Picturebox pbx...?
Yeah, use this.Controls.
This wont work though:
C# Code:
foreach(PictureBox p in this.Controls)
{
}
Because it will visit every control in the form container and implicitly try to cast them to Pictureboxes, upon reaching a control that isnt a PictureBox, an exception will occur. You'd have to do:
C# Code:
foreach(Control c in this.Controls)
{
if(c is PictureBox)
((PictureBox)c).Image = Something
}
To get around that hassle, it's easier to use an array of PictureBoxes. Its much more effective, as you wont have to loop through every control to find the pictureboxes.
Edit: Oh my, shouldve refreshed before posting.
-
Jan 22nd, 2008, 02:35 PM
#6
Thread Starter
Lively Member
Re: [3.0/LINQ] Foreach (Picturebox pbx...?
Aha!
Thank you both for answering, specially Atheist for giving detailed information as well! Greatly appreciated! (:
Cheers,
Zolomon
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|