How to access controls that is in the panel in c#
39,866
Solution 1
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
- PictureBox is followed by a period (.)
- 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);
Solution 2
Try this:
foreach (Control p in panal.Controls)
{
if (p is PictureBox)
{
p.Left = 50;
}
}
Solution 3
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;
}
}
Solution 4
I think
foreach (PictureBox p in panel.Controls.OfType<PictureBox>())
{
p.Location = new Point(50, p.Location.Y);
}
could be solution too.
Author by
qulzam
Updated on June 03, 2020Comments
-
qulzam almost 4 years
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'
-
Fredrik Mörk over 14 years+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.
-
tina Miller over 14 yearsCould also use Controls.OfType<PictureBox>(), not that it shortens the code.
-
qulzam over 14 yearsi 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 over 14 yearsin 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 over 14 yearsThank 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?
-
MusiGenesis over 14 yearsI can't explain it, but it would be a good StackOverflow question.
-
MusiGenesis over 14 yearsYeah, 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.
-
Zeeshanef almost 10 yearsCast known control as PictureBox:
If (p is PictureBox) { PictureBox pb = (PictureBox)p }