如果更改控件的名称,如何使用新名称访问它? [英] If you change the name of a control how do you access it using the new name?

查看:54
本文介绍了如果更改控件的名称,如何使用新名称访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建可能会更改的基于控件的项目,然后将控件添加到用户必须能够操作的那些项目中.该程序必须能够从这些控件中收集和保存数据.

如果在运行时更改控件的名称,如何使用新名称访问它?

这是我正在玩的测试代码

标签test1 = new Label();
test1.Name ="test2";
test2.Name ="test1";

即使我有显示其名称的东西,test2也不能用来对标签做任何事情.

更改名称或使用更动态的名称开始后,我需要能够使用控件.这是我实际上正在使用并且可以正常工作的代码,但是我离开createTab方法后无法更改或从控件获取数据,即使在只有tab和lst工作的情况下,重命名后也不允许我使用例如lstStrength但仍然可以让我使用lst.

I need to create controls based items that may change and then add controls to those items that user has to be able to manipulate. The program has to be able to collect and save data from those controls.

If you change the name of a control at run time how do you access it using the new name?

here was the test code I was playing with

Label test1 = new Label();
test1.Name = "test2";
test2.Name = "test1";

test2 cannot be used to do anything to the label even though if I have something display its name that is what it will display.

I need to be able to work with the controls after I change the name or using a more dynamic name to start with. this is the code I have that i am actually using and works but I cannot change or get data from the controls after I leave createTab method and even in there only tab and lst work it dose not let me use for example lstStrength once I rename it but it will still let me use lst.

private void FrmCharGen_Load(object sender, EventArgs e)
{
    createTab("Strength");
    createTab("Agility");
    createTab("perception");
    createTab("Intelegence");
    createTab("social");
    createTab("memory");
}
private void createTab(string tabName)
{
    string skillName = "test";
    TabPage tab = new TabPage(Text = tabName);
    ListBox lst = new ListBox();
    tabCharDescAttr.TabPages.Add(tab);
    tab.Controls.Add(lst);

    lst.Items.Add(skillName);

    tab.Name = "tab" + tabName;
    lst.Name = "lst" + tabName;

}

推荐答案

您可以查看容器的控件集合.

假设tabCharDescAttr是一个TabControl,并且您想获得特定的TabPage使用;
You have a look at the controls collection of the container.

Assuming that tabCharDescAttr is a TabControl and you want to get a particular TabPage use;
TabPage tab = ((TabPage)tabCharDescAttr.Controls[NameOfTabPage])



假设您已完成上述操作,则从TabPage获取ListBox的操作也是如此;



The same goes for getting the ListBox from the TabPage, assuming you have done the above;

ListBox lst = ((ListBox)tab.Controls[NameOfListBox])


这篇关于如果更改控件的名称,如何使用新名称访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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