如何访问C#面板中的控件 [英] How to access controls that is in the panel in c#

查看:108
本文介绍了如何访问C#面板中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#winforms中使用一个面板,并使用循环将图片框的编号填充到面板中

I use a panel in c# winforms and fill the panel with the no of picture box using loop

例如,面板名称为panal

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;

但是有错误。错误是:

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


推荐答案

本节中似乎有一些错别字(可能

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;

错别字是


  1. PictureBox之后是句点(。)

  2. GetType缺少括号(因此未调用)。

错误是:


  • 您无法比较 p 到PictureBox,则需要将其与PictureBox的类型进行比较。

  • You can't compare the type of p to PictureBox, you need to compare it to the type of 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);

这篇关于如何访问C#面板中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆