且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中访问动态创建的控件的属性

更新时间:2023-10-12 21:42:04

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



然后使用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;
}
}


请尝试以下代码:



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