Results 1 to 6 of 6

Thread: [RESOLVED] [3.0/LINQ] Foreach (Picturebox pbx...?

  1. #1

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Resolved [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

  2. #2
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    Re: [3.0/LINQ] Foreach (Picturebox pbx...?

    you loop through the control container not the form itself

  3. #3

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    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

  4. #4
    Addicted Member
    Join Date
    Jun 2003
    Location
    Birmingham, AL
    Posts
    188

    Re: [3.0/LINQ] Foreach (Picturebox pbx...?

    Code:
    foreach (Control c in this.Controls)
    {
      if (c is PictureBox)
      {
        // do code
      }
    }

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [3.0/LINQ] Foreach (Picturebox pbx...?

    Yeah, use this.Controls.
    This wont work though:
    C# Code:
    1. foreach(PictureBox p in this.Controls)
    2. {
    3. }
    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:
    1. foreach(Control c in this.Controls)
    2. {
    3.     if(c is PictureBox)
    4.         ((PictureBox)c).Image = Something
    5. }
    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    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
  •  



Click Here to Expand Forum to Full Width