在运行时将控件添加到c#用户控件并能够对其进行管理 [英] Add controls to a c# user control at runtime and be able to manage them

查看:80
本文介绍了在运行时将控件添加到c#用户控件并能够对其进行管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

1)
我开发了一个C#用户控件.
在该控件中,我有一个按钮.当用户在运行时单击按钮时,将创建一个新控件(例如pictureBox),该控件位于上一个pictureBox的旁边.

我是这样做的:

Hello,

1)
I develop a c# user control.
In that control, I have a button. When the user clicks the button at runtime, a new control (for example, pictureBox) is created ,next to the previous pictureBox.

I did it that way:

PictureBox pb = new PictureBox();
pb.Location = new Point(oldPb.X, oldPb.Y + 100);
pb.Size = oldPb.Size;
Controls.Add(pb);



问题是,我希望能够管理所有创建的项目.
例如,我想为pictureBox编制索引,然后从用户那里获取一个数字并更改所需photoBox的照片.
例如:



The problem is, that I want to be able to manage all of the created items.
I want, for example, to index the pictureBoxes, then get a number from the user and change the photo of the wanted photoBox.
for example:

photoBox3.Image = .......



我该怎么办?

2)
我希望能够识别用户何时单击这些photoBox之一并对选定的photoBox进行操作.
我该怎么办?

谢谢



How can I do it?

2)
I want to be able to recognize when the user clicks on one of those photoBoxes and do an action on the chosen photoBox.
How can I do that?

Thanks

推荐答案

向您添加列表UserControl:
Add a List to you UserControl:
private List<PictureBox> Pictures = new List<PictureBox>();



然后,将其添加到控件列表中时,还将其添加到图片"中:



Then when you add it to the controls list, you also add it to the Pictures:

PictureBox pb = new PictureBox();
pb.Location = new Point(oldPb.X, oldPb.Y + 100);
pb.Size = oldPb.Size;
Controls.Add(pb);
Pictures.Add(pb);

当您要访问它时,可以按添加它们的顺序按索引进行操作:

When you want to access it, you can do it by index, in the order you added them:

PictureBox toChange = Pictures[3];

(显然,您需要确保列表中有足够的PictureBoxes加上Count属性,然后再尝试获取它!)

(Obviously, you will want to make sure there are enough PictureBoxes in the list with the Count property before you try to get it!)


这篇关于在运行时将控件添加到c#用户控件并能够对其进行管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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