How to access controls in a panel in C #

I use the panel in winforms C # and fill the panel with a drawer without an image using a loop

For example, the panel name is Panama.

foreach (string s in fileNames)
{            
    PictureBox pbox = new new PictureBox();
    pBox.Image = Image.FromFile(s);
    pbox.Location = new point(10,15);
    .
    .
    .
    .
    this.panal.Controls.Add(pBox);
}

Now I want to change the location of the image window in another method. The problem is that now I can access the images to change their location. I am trying to use the following, but this is not a success.

foreach (Control p in panal.Controls)
                if (p.GetType == PictureBox)
                   p.Location.X = 50;

But there is a mistake. Mistake:

System.Windows.Forms.PictureBox' is a 'type' but is used like a 'variable'
+5
source share
7 answers

There seems to be some typos in this section (and maybe a real mistake).

foreach (Control p in panal.Controls)
                if (p.GetType == PictureBox.)
                   p.Location.X = 50;

Typos

  • PictureBox is followed by a period (.)
  • GetType skips parens (therefore it is not called).

Mistake:

  • p PictureBox, PictureBox.

:

foreach (Control p in panal.Controls)
   if (p.GetType() == typeof(PictureBox))
      p.Location = new Point(50, p.Location.Y);

:

foreach (Control p in panal.Controls)
   if (p is PictureBox)
      p.Location = new Point(50, p.Location.Y);
+18

:

foreach (Control p in panal.Controls)
{
    if (p is PictureBox)
    {
        p.Left = 50;
    }
}
+4

panel.Controls
 //^ this is an 'e'

panal.Controls?
 //^ this is an 'a'
0

p.GetType == PictureBox ( )... GetType /, , p.GetType()

0

for.

foreach (Control p in panel.Controls)
{
  if (p is PictureBox) // Use the keyword is to see if P is type of Picturebox
  {
     p.Location.X = 50;
  }
}
0

picturebox , - , .

0

,

foreach (PictureBox p in panel.Controls.OfType<PictureBox>())
        {
            p.Location = new Point(50, p.Location.Y);
        }

.

0

All Articles