如何从 List访问更派生的控件类的属性?
我发现无法在 List
中使用 PictureBox
控件的 Image
属性。
我想做这样的事情:
List<Control> pictureboxes = new List<Control>();
private void button1_Click(object sender, EventArgs e)
{
foreach (var picturebox in pictureboxes)
{
picturebox.Image = WindowsFormsApplication1.Properties.Resources.image;
}
}
我能以某种方式做到这一点吗?
I found that I can't use the Image
property for a PictureBox
control in List<Control>
.
I want to do something like this:
List<Control> pictureboxes = new List<Control>();
private void button1_Click(object sender, EventArgs e)
{
foreach (var picturebox in pictureboxes)
{
picturebox.Image = WindowsFormsApplication1.Properties.Resources.image;
}
}
Can I somehow do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将
PictureBox
控件放入List
容器中时,无法访问它们的Image
属性的原因是列表的基本类型 (Control
) 没有Image
属性。不过,该财产并没有消失。您只需将对象从
Control
转换为更派生的类PictureBox
。然后您可以调用任何方法或访问您想要的任何属性。例如:The reason that you can't access the
Image
property of yourPictureBox
controls when you place them in aList<Control>
container is because the base type of the list (Control
) doesn't have anImage
property.The property didn't disappear, though. You just have to cast the object from a
Control
to a more derived class,PictureBox
. Then you can call whatever methods or access whatever properties you want. For example: