最近公司交了一个任务,修改一个项目达到想要的效果,进去以后发现取值是一个问题,各种无法公用实例化,后来仔细看发现原来之前写代码的用的是Controls来动态添加控件,后来发下原来我们需要去foreach循环Controls这个控件集,然后去遍历所有的控件,然后在遍历完了以后找到我们所需要的控件,这样就可以找到我们需要的值了

ComboBoxEx VariType = new ComboBoxEx();
        ComboBoxEx VariMode = new ComboBoxEx();
        TextBox VariValue = new TextBox();
        int i = 0;

        private void buttonX1_Click(object sender, EventArgs e)
        {
           
            i++;
            //VariablesForm
            ComboBoxEx VariType = new ComboBoxEx();
            ComboBoxEx VariMode = new ComboBoxEx();
            TextBox VariValue = new TextBox();
            try
            {
                #region 变量类型参数
                VariType.DropDownStyle = ComboBoxStyle.DropDownList;
                VariType.Items.AddRange(Default.VariableType);
                VariType.SelectedIndex = 0;
                VariType.Width = 95;
                VariType.Top = buttonX1.Top;
                VariType.Left = 12;
                VariType.Name = "VariType" + i.ToString();
                this.Controls.Add(VariType);
                #endregion
                #region 变量值
                VariValue.Width = 160;
                VariValue.Top = buttonX1.Top;
                VariValue.Left = 113;
                VariValue.Name = "VariValue" + i.ToString();
                this.Controls.Add(VariValue);
                #endregion
                #region 变量mode
                VariMode.DropDownStyle = ComboBoxStyle.DropDownList;
                VariMode.Items.AddRange(Default.VariableMode);
                VariMode.SelectedIndex = 0;
                VariMode.Width = 65;
                VariMode.Top = buttonX1.Top;
                VariMode.Left = 280;
                VariMode.Name = "VariMode" + i.ToString();
                this.Controls.Add(VariMode);
                #endregion
                buttonX1.Top = buttonX1.Top + 37;
                buttonX2.Top = buttonX2.Top + 37;
                this.Height = this.Height + 37;
                //this.Size = new Size(300,);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); ;
            }
        }
 private void buttonX2_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.Controls)
            {
                if (control.Name == "VariType1")
                {
                    InitForm.VarType.Add(control.Text.ToString());
                }
                else if (control.Name == "VariType2")
                {
                    InitForm.VarType.Add(control.Text.ToString());
                }
                else if (control.Name == "VariValue1")
                {
                    InitForm.VarValue.Add(control.Text.ToString());
                }
                else if (control.Name == "VariValue2")
                {
                    InitForm.VarValue.Add(control.Text.ToString());
                }
                else if (control.Name == "VariMode1")
                {
                    InitForm.VarMode.Add(control.Text.ToString());
                }
                else if (control.Name == "VariMode2")
                {
                    InitForm.VarMode.Add(control.Text.ToString());
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }