How to access controls that is in the panel in c#
Asked Answered
F

7

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'
Fixer answered 11/8, 2009 at 13:18 Comment(3)
What is the error? Also is this WPF or Winforms? The more detail you give the more likely you will get the answer you are looking for.Outstrip
I use the winforms and the error is System.Windows.Forms.PictureBox' is a 'type' but is used like a 'variable'Fixer
@qulzam: The code of C. Ross will resolve your issueDosage
G
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);
Gun answered 11/8, 2009 at 13:24 Comment(3)
+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.Producer
Could also use Controls.OfType<PictureBox>(), not that it shortens the code.Drubbing
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 variableFixer
M
4

Try this:

foreach (Control p in panal.Controls)
{
    if (p is PictureBox)
    {
        p.Left = 50;
    }
}
Manchukuo answered 11/8, 2009 at 14:3 Comment(4)
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?Fixer
I can't explain it, but it would be a good StackOverflow question.Manchukuo
i think that PictureBox.Location.x is read only property . so we cannot change or write it.Fixer
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.Manchukuo
O
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;
  }
}
Outstrip answered 11/8, 2009 at 13:26 Comment(2)
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 variableFixer
Cast known control as PictureBox: If (p is PictureBox) { PictureBox pb = (PictureBox)p }Spiegeleisen
C
1

I think

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

could be solution too.

Carcinoma answered 14/2, 2017 at 23:38 Comment(0)
A
0

Don't you want

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

instead of

panal.Controls?
 //^ this is an 'a'
Aho answered 11/8, 2009 at 13:22 Comment(2)
They would... but that isn't causing an error, that is simply the name of the control and is spelling agnostic.Broadcloth
Well, I cannot know that if he does not say what error he getsAho
B
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()

Broadcloth answered 11/8, 2009 at 13:23 Comment(0)
T
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.

Thiourea answered 11/8, 2009 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.