如何在c#中访问面板中的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1260296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:21:40  来源:igfitidea点击:

How to access controls that is in the panel in c#

c#winformsdesktop-application

提问by qulzam

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

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

For example, panel name is panal

例如,面板名称是 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'

采纳答案by C. Ross

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).
  1. PictureBox 后跟一个句点 (.)
  2. GetType 缺少括号(因此未调用)。

The error is:

错误是:

  • You can't compare the type of pto PictureBox, you need to compare it to the type of PictureBox.
  • 您无法将p的类型与 PictureBox 进行比较,您需要将其与 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);

回答by qulzam

Don't you want

你不想要

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

instead of

代替

panal.Controls?
 //^ this is an 'a'

回答by Fooberichu

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()

在您的第二个块中 p.GetType == PictureBox 之后的句号是错误的(此处不需要句号)...就此而言,GetType 是一个方法/函数而不是一个属性,因此它需要是 p.GetType()

回答by David Basarab

Next there might be some bugs in your for loop.

接下来,您的 for 循环中可能存在一些错误。

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;
  }
}

回答by tom.dietrich

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.

您最好将图片框设为表单本身的私有变量,这样您就可以使用它进行操作,而不必每次都遍历面板的控件。

回答by MusiGenesis

Try this:

尝试这个:

foreach (Control p in panal.Controls)
{
    if (p is PictureBox)
    {
        p.Left = 50;
    }
}

回答by Birkan Tu?cu

I think

我认为

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

could be solution too.

也可以是解决方案。