如何在C#中访问动态创建的控件的属性 [英] How Do I Access A Dynamically Created Control's Properties In C#

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

问题描述

我正在创建一个按钮实例,使用循环nad发现很难修改粒子按钮实例的属性/搜索整个互联网nad无法找到解决方案。任何形式的帮助或链接到帮助来源表示赞赏。



这里是我目前的代码



Button btn;

void CreateButtons()

{

for(int i = 0; i< 10; i ++)

{

btn =新按钮;

btn.Size =新尺寸(30,30);

btn.Tag = i.ToString();

flowlayoutPanel.Controls.Add(btn);

}

}



是否有访问例如第一个按钮本身而不是访问其标签属性。例如

btn_Something.BackColor = Color.Blue;

解决方案

在这种情况下,由于你动态创建它们,你需要给它们一个唯一的名称,然后您可以通过名称从控件列表中获取控件。



然后使用ContainsKey(键)从ControlCollection索引搜索控件列表中的名称控件名称为键。但是,如果需要,您也可以遍历集合中的所有控件。



  void  CreateButtons()[
for int i = 0 ; i < 10 ; i ++){
Button btn = new Button();
btn.Size = new 大小( 30 30 );
btn.Name = CreateButton + i.ToString();

// 可能需要进行一些检查以确保Name / btn不会已经存在。
if (!flowLayoutPanel.Controls.ContainsKey(btn.Name)){
flowLayoutPanel.Controls.Add( BTN);
}
}
}

void SetButtonBackColor( string btnName,Color color){
Button btn = null ;
if (flowLayoutPanel.Controls.ContainKey(btnName)){
// 使用as cast,因为如果它不能转换为Button,它将返回null。
// 在你的情况下不应该发生,但永远不会知道:)
btn = flowLayoutPanel.Controls [btnName] as 按钮;
}

if null != btn){
// DO Stuff
btn.BackColor = color;
}
}


请尝试以下代码:



< pre lang =cs> void CreateButtons()
{
for int i = 0 ; i< 10; i ++)
{
Button btn = new Button();
btn.Size = new 大小( 30 30 );
btn.Tag = i.ToString();
btn.BackColor = Color.Blue;
flowLayoutPanel.Controls.Add(btn);
}
}





希望有所帮助,

Dudi Avrahamov
软件顾问

il.linkedin.com/in/dgsoft


I am creating an instance of a button using a loop nad finding it hard to modify the properties of a particlar button instance/ Have searched the whole internet nad failed to get to the solution. Any form of help or links to help sources is appreciated.

here is the code that i have presently

Button btn;
void CreateButtons()
{
for(int i = 0; i<10; i++)
{
btn = new Button;
btn.Size = new Size(30,30);
btn.Tag = i.ToString();
flowlayoutPanel.Controls.Add(btn);
}
}

Is there a was of accessing for example the first button itself rather than accessing its tag property. for example
btn_Something.BackColor = Color.Blue;

解决方案

In this case since your creating them dynamically, you need to give them a unique name, then you can get the control from the control list by the name.

Then search the control list for the name using ContainsKey(key) since the ControlCollection indexes the control Names as the key. However you can also iterate through all the control in the collection if you desired.

void CreateButtons() [
    for (int i = 0; i < 10; i++) {
        Button btn = new Button();
        btn.Size = new Size(30,30);
        btn.Name = "CreateButton" + i.ToString(); 

        // might need to do some checking to make sure the Name/btn doesn't already exist.
        if (!flowLayoutPanel.Controls.ContainsKey(btn.Name)) {
            flowLayoutPanel.Controls.Add(btn);
        }
    }
}

void SetButtonBackColor(string btnName, Color color) {
    Button btn = null;
    if (flowLayoutPanel.Controls.ContainKey(btnName)) {
        // use the as cast since it will return null if it can't be cast to Button.
        // In your case shouldn't happen, but never know :)
        btn = flowLayoutPanel.Controls[btnName] as Button;
    }

    if (null != btn) {
        // DO Stuff
        btn.BackColor = color;
    }
}


Try the following code:

void CreateButtons()
  {
      for(int i = 0; i<10; i++)
      {
          Button btn = new Button();
          btn.Size = new Size(30,30);
          btn.Tag = i.ToString();
          btn.BackColor = Color.Blue;
          flowLayoutPanel.Controls.Add(btn);
      }
  }



Hope it helps,
Dudi Avrahamov
Software Consultant
il.linkedin.com/in/dgsoft


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

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