1

I have a form with a tabbed control (5 tabs) and on one tab I have 10 picture boxes, named pic1, pic, pic3 and so on. In VBA it is possible to loop through the controls using something like this:

 For i = 1 To 10
    Me.Controls("Img" & i).Picture = Me.cboProperty.Column(i)
 Next

At the moment I have

 this.pic1.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic1");
 this.pic2.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic2");
 this.pic3.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic3");

until 10, but surely there is a better way?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
PeterJ
  • 364
  • 4
  • 17

5 Answers5

4

Like that, no. Normally, you would acceess a group of controls like this:

foreach (var pictureBox in PictureTab.Controls.OfType(Of PictureBox)())
{
   pictureBox.Picture = //...
}

Of course, the trick here is that there is no index, to know which column to look at. But that's easy enough to get around:

int i = 0;
foreach (var pictureBox in PictureTab.Controls.OfType<PictureBox>())
{
   pictureBox.Picture = cboProperty.Column[i];
   i++;
}

Based on your edit, you may want this:

int i = 0;
foreach(var pictureBox in PictureTab.Controls.OfType<PictureBox>())
{
    picutreBox.DataBindings.Add("ImageLocation", mainTableBindingSource, string.Format("loalPic{0}", i));
    i++;
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

Perhaps something like this:

 var pictureBoxes = this.Controls.OfType<PictureBox>().ToList();
 for (int i = 0; i < pictureBoxes.Count; i++) {
     pictureBoxes[i].DataBindings.Add("ImageLocation", 
                                      this.mainTableBindingSource, 
                                      "localPic" + (i + 1).ToString());
 }
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

You can Search for the name of the pictureBox in form controls and loop increasing the value of each like Pic1, Pic2, etc.

for (int a = 1; a <= 10; a++)
{
    string name = "pic" + a;
    PictureBox pic = (PictureBox)(this.Controls.Find(name, true))[0];
    pic.DataBindings.Add("ImageLocation", this.mainTableBindingSource, "localPic3");
}
  • 1
    I guess more than one of the proposed methods would work, but I selected Tijesunimi's simple yet effective method. Thanks for all your help. – PeterJ Dec 02 '13 at 22:18
  • @Peter Sadly, Controls.Find() is about the worst way to do this of those proposed. It's slower, and relies on magic-strings. – Joel Coehoorn Dec 02 '13 at 22:19
0

iterate through all form controls

List<Control> lstOfControls = new List<Contol>();
public void AllPictureControls(Control control, bool enabled) 
{
     foreach(Control child in control.Controls)
     {
         if(child.Name.StartsWith("pic"))
         {
             lstOfControls.Add(control);
         }
     }
}
Silvermind
  • 5,791
  • 2
  • 24
  • 44
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
-1

Put references of all your picture controls into a data structure, e.g. an array, and iterate over them.

Something similar to:

PictureBox[] picBoxArray = new PictureBox[50];

// 50 iterations for 50 object instantiations.
for (int i=0; i<picBoxArray.Length; i++) {

    // create an pictureBox instance.
    picBoxArray[i] = new PictureBox();

}

PictureBox pb = null;

// 50 iterations for calling methods on each of the 50 objects.
for (int i=0; i<picBoxArray.Length; i++) {

    pb = picBoxArray[i];
    // do something with the picturebox e.g. pb.SetPicture(...)

}
marc wellman
  • 5,808
  • 5
  • 32
  • 59