我在 c# winforms 中使用面板,并使用循环用图片框的编号填充面板
例如面板名称为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);
}
现在我想用另一种方法更改图片框的位置。
问题是现在我如何访问图片框以便更改它们的位置。
我尝试使用以下内容,但没有成功。
foreach (Control p in panal.Controls)
if (p.GetType == PictureBox)
p.Location.X = 50;
但有一个错误。错误是:
System.Windows.Forms.PictureBox' is a 'type' but is used like a 'variable'
本节中似乎存在一些拼写错误(并且可能是真正的错误)。
foreach (Control p in panal.Controls)
if (p.GetType == PictureBox.)
p.Location.X = 50;
错别字是
- PictureBox 后跟一个句点 (.)
- GetType 缺少括号(因此未调用它)。
错误是:
- 你无法比较类型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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)