Results 1 to 8 of 8

Thread: [RESOLVED] accessing controls in a loop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [RESOLVED] accessing controls in a loop

    Hi,
    I have 6 pictureboxes on my form to display random pictures in.

    I use several functions to load pictures, clear pictures etc. All of these functions have all 6 pictureboxes hardcoded.

    For example:
    Code:
    private void ShowPics()
    {
      this.pictureBox1.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
      this.pictureBox2.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
      this.pictureBox3.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
      this.pictureBox4.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
      this.pictureBox5.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
      this.pictureBox6.Image = this.pictures[this.rnd.Next(0, this.pictures.Length)];
    }
    So if I wanted to add more pictureboxes I'd have to add lines to all those functions (for pictureBox6, 7, ...). Is there a more dynamic way to access each picturebox in a loop or arrayList or something?
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: accessing controls in a loop

    How about an For i = 0 To 5 loop?

  3. #3
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: accessing controls in a loop

    hi look at this example:

    Code:
    foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is PictureBox)
                    {
                        MessageBox.Show(ctrl.Name);
                    }
                
                }
    this will display al the picturebox names in your form.so like this u can loop through your contols in your page
    Loving dotnet

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: accessing controls in a loop

    Hack,
    How would you use the for loop with separate controls?

    karthikeyan,
    This works for .Name but not for .Image of the picturebox
    Thanks

    Tomexx.

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: accessing controls in a loop

    That's because it's still being seen as an object. Try something more like this:
    csharp Code:
    1. foreach (Control ctrl in this.Controls)
    2. {
    3.    if (ctrl is PictureBox)
    4.    {
    5.       PictureBox pb = (PictureBox)ctrl; //cast it as a PictureBox to expose the right properties
    6.       MessageBox.Show(pb.Image);
    7.    }
    8. }

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: accessing controls in a loop

    Casting worked. Thanks.

    I was hoping that I wouldn't have to go through all controls to find the boxes. I wonder how efficient would it be with lots of controls on the form.
    Thanks

    Tomexx.

  7. #7
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [RESOLVED] accessing controls in a loop

    No problem. Casting is an essential in C#. Probably the majority of type operations will involve some form of cast, otherwise C# won't know what type of object it's dealing with. Rather, it'll be dealing with the object type itself, and not the type you want it to.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] accessing controls in a loop

    If you want to loop through a number of controls then create an array of controls in code:
    Code:
    private PictureBox[] pictureBoxes;
    
    public Form1()
    {
        InitializeComponent();
    
        this.pictureBoxes = new PictureBox[] { this.pictureBox1,
                                               this.pictureBox2,
                                               this.pictureBox3 };
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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