views:

970

answers:

6

I use a panel in c# winforms and fill the panel with the no of picture box using loop. like this eg 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 anothe mathd. The problem is that how can now i access the pictureboxes so that i change the location of them. i try to use the followin but it is not success.

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

But there is error. The error is:

System.Windows.Forms.PictureBox' is a 'type' but is used like a 'variable'

Plz help!

A: 

Don't you want

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

instead of

panal.Controls?
 //^ this is an 'a'
Time Machine
They would... but that isn't causing an error, that is simply the name of the control and is spelling agnostic.
Fooberichu
Well, I cannot know that if he does not say what error he gets
Time Machine
Fooberichu , u r right
qulzam
A: 

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
+9  A: 

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
+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
Could also use Controls.OfType<PictureBox>(), not that it shortens the code.
Benjol
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
Right you are, one moment.
C. Ross
yes, now correct. Thanks Ross for very quick answers.
qulzam
A: 

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
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
A: 

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
+2  A: 

Try this:

foreach (Control p in panal.Controls)
{
    if (p is PictureBox)
    {
        p.Left = 50;
    }
}
MusiGenesis
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
I can't explain it, but it would be a good StackOverflow question.
MusiGenesis
i think that PictureBox.Location.x is read only property . so we cannot change or write it.
qulzam
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