从列表中获取新创建的对象

本文关键字:创建 对象 列表 获取 新创建 | 更新日期: 2023-09-27 18:25:10

我正在自动创建控件,我想将一些满足特定条件的特殊控件添加到另一个控件列表中。我该怎么做?请参阅伪代码中的注释。

List<Control> comboBOX;
List<Control> othercomboBOX;
    case controls.LIST:
                     comboBOX.Add(new SpecialComboBox(someparam));
                     panel.Controls.Add(comboBOX[i])
                     //Now i have this statement below:
                     if(somecondition)
                      {
                         //Take the newly created combobox which has been added to comboBOX list and add it into another list *othercomboBOX*;
                        othercomboBOX.Add(the newly created combobox)
                      }break;

从列表中获取新创建的对象

只需将new SpecialComboBox粘贴到一个变量中,并将其添加到两个列表中。

List<Control> comboBOX;
List<Control> otherComboBOX;
case controls.LIST:
    ComboBox specialComboBox= new SpecialComboBox(someparam);
    comboBOX.Add(specialComboBox);
    panel.Controls.add(comboBOX[i]);
     //Now i have this statement below:
         if(somecondition) {
             //Take the newly created combobox which has been added to comboBOX list and add it into another list *othercomboBOX*;
            othercomboBOX.Add(specialComboBox);
         }break;

正如我正确理解的那样,您希望获得新创建的ComboBox。所以,只需像comboBOX[i]那样使用索引从List<Control> comboBOX中提取一个组合框,并添加到othercomboBox.Add(comboBox[i]):

case controls.LIST:
                 comboBOX.Add(new SpecialComboBox(someparam));
                 panel.Controls.Add(comboBOX[i])
                 //Now i have this statement below:
                 if(somecondition)
                  {
                     //Take the newly created combobox which has been added to comboBOX list and add it into another list *othercomboBOX*;
                    othercomboBOX.Add(comboBOX[i])
                  }break;