7

I use a panel in c# winforms and fill the panel with the no of picture box using loop

For example, panel name is panal

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 picturebox in another method. The problem is that how can now I access the pictureboxes so that I change the location of them. I try to use the following but it is not the success.

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

But there is an error. The error is:

System.Windows.Forms.PictureBox' is a 'type' but is used like a 'variable'
gorkem
  • 731
  • 1
  • 10
  • 17
qulzam
  • 315
  • 2
  • 9
  • 20

7 Answers7

24

There appear to be some typos in this section (and possibly a real error).

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

The typos are

  1. PictureBox is followed by a period (.)
  2. GetType is missing the parens (so it isn't called).

The error is:

  • You can't compare the type of p to PictureBox, you need to compare it to the type of PictureBox.

This should be:

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

Or simply:

foreach (Control p in panal.Controls)
   if (p is PictureBox)
      p.Location = new Point(50, p.Location.Y);
C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • +1. In order to make the answer complete I guess it could be a good idea to point out the typos and errors in the original code snippet. – Fredrik Mörk Aug 11 '09 at 13:28
  • Could also use Controls.OfType(), not that it shortens the code. – Benjol Aug 11 '09 at 13:28
  • in the line (p.Location.X = 50;) is err i.e Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable – qulzam Aug 11 '09 at 14:13
4

Try this:

foreach (Control p in panal.Controls)
{
    if (p is PictureBox)
    {
        p.Left = 50;
    }
}
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • Thank MusiGenesis. i solve it. Still i have confision the why ( p.x = 50; ) is wrong and give error. if we use the ( p.Location = new point(50,10); ) it is right. i think that new point is also equal to x and y values. can any one explain this? – qulzam Aug 11 '09 at 14:31
  • I can't explain it, but it would be a good StackOverflow question. – MusiGenesis Aug 11 '09 at 14:45
  • i think that PictureBox.Location.x is read only property . so we cannot change or write it. – qulzam Aug 11 '09 at 15:02
  • Yeah, but you asked a question I never really thought about: why are X and Y in a Point (or any control) read-only? There's probably a good reason, but I've never encountered it. – MusiGenesis Aug 11 '09 at 15:38
1

Next there might be some bugs in your for loop.

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;
  }
}
David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • i recevid the following err in line p.Location.x = 50; Error 1 Cannot modify the return value of 'System.Windows.Forms.Control.Location' because it is not a variable – qulzam Aug 11 '09 at 13:40
  • Cast known control as PictureBox: `If (p is PictureBox) { PictureBox pb = (PictureBox)p }` – Zeeshanef Jun 16 '14 at 15:22
1

I think

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

could be solution too.

Birkan Tuğcu
  • 11
  • 1
  • 5
0

Don't you want

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

instead of

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

In your second block the period after p.GetType == PictureBox is wrong (no period required here)... for that matter, GetType is a method/function not a Property so it needs to be p.GetType()

Fooberichu
  • 428
  • 1
  • 5
  • 13
0

You would be better off making the picturebox a private variable of the form itself, so that you could do things with it without having to step though the panel's controls every time.

tom.dietrich
  • 8,219
  • 2
  • 39
  • 56