如何获取在运行时创建的面板名称. [英] How to get panel name that was created at run time.

查看:55
本文介绍了如何获取在运行时创建的面板名称.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在运行时创建了面板.使用
在已创建的面板上添加这些面板 Panel1.controls.Add(p1 [i]);
我在运行时创建了n个面板,并添加了这些面板.当我单击在运行时创建的面板时,我需要一个值i.或需要知​​道面板的名称.如果知道,请帮助我.

Hi,
I am created the panels at run time. Add''d those panels at already created panel using
Panel1.controls.Add(p1[i]);
I created a n number of panels at run time and added those add panel1. When i click the panel created at run time i need a value of i. or Need to know name of panel. If know means please help me.

推荐答案

您可以将click事件处理程序添加到面板中:

You can add the click event handler to the panel:

p1[i].Click += DynamicPanels_Click;



点击事件如下所示:



The click event would look like this:

private void DynamicPanels_Click(object sender, EventArgs e)
{
    (sender as Panel).Name;   // get the name
}


您将需要为面板定义一个名称-因为这些是动态面板.
您可以在创建它们时执行此操作-通过Name 属性.
You will need to define a name to the panel yourself - since these are dynamic panels.
You can do this when you are creating them - via the Name property.


您可以将i置于Tag属性中.或者,您可以将Name属性设置为另外两个解决方案.

通常更好的是存储对面板的引用(以List< Panel>或类似方式)并在其中查找,而不是依靠数字.或者,如果您想要数字的原因是要在另一个数据结构中查找内容,则将对目标对象的引用存储在Tag中.
You can put i in the Tag property. Or you can set the Name property as the other two solutions mention.

What is generally better is to store a reference to the panels (in a List<Panel> or similar) and look them up in there, instead of relying on numbers. Or if the reason you want the number is to look something up in another data structure, store a reference to the target object in Tag.


这篇关于如何获取在运行时创建的面板名称.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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