//接上面 // 
// cmbLogic0
// 
this.cmbLogic0.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbLogic0.Items.AddRange(new object[] {
   "并且",
   "或者"});
this.cmbLogic0.Location = new System.Drawing.Point(312, 62);
this.cmbLogic0.Name = "cmbLogic0";
this.cmbLogic0.Size = new System.Drawing.Size(48, 20);
this.cmbLogic0.TabIndex = 4;
this.cmbLogic0.SelectedIndexChanged += new System.EventHandler(this.cmbLogic0_SelectedIndexChanged);
// 
// lblIsValid
// 
this.lblIsValid.Location = new System.Drawing.Point(8, 44);
this.lblIsValid.Name = "lblIsValid";
this.lblIsValid.Size = new System.Drawing.Size(32, 16);
this.lblIsValid.TabIndex = 5;
this.lblIsValid.Text = "有效";
// 
// lblField
// 
this.lblField.Location = new System.Drawing.Point(55, 44);
this.lblField.Name = "lblField";
this.lblField.Size = new System.Drawing.Size(56, 16);
this.lblField.TabIndex = 6;
this.lblField.Text = "字段名字";
// 
// lblValue
// 
this.lblValue.Location = new System.Drawing.Point(224, 44);
this.lblValue.Name = "lblValue";
this.lblValue.Size = new System.Drawing.Size(37, 16);
this.lblValue.TabIndex = 7;
this.lblValue.Text = "取值";
// 
// lblLogic
// 
this.lblLogic.Location = new System.Drawing.Point(312, 44);
this.lblLogic.Name = "lblLogic";
this.lblLogic.Size = new System.Drawing.Size(56, 16);
this.lblLogic.TabIndex = 8;
this.lblLogic.Text = "邏輯條件";
// 
// btnQuery
// 
this.btnQuery.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnQuery.Image = ((System.Drawing.Bitmap)(resources.GetObject("btnQuery.Image")));
this.btnQuery.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnQuery.Location = new System.Drawing.Point(0, 4);
this.btnQuery.Name = "btnQuery";
this.btnQuery.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.btnQuery.Size = new System.Drawing.Size(56, 23);
this.btnQuery.TabIndex = 9;
this.btnQuery.Text = "查詢";
this.btnQuery.TextAlign = System.Drawing.ContentAlignment.TopLeft;
// 
// btnFresh
// 
this.btnFresh.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnFresh.Image = ((System.Drawing.Bitmap)(resources.GetObject("btnFresh.Image")));
this.btnFresh.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnFresh.Location = new System.Drawing.Point(56, 4);
this.btnFresh.Name = "btnFresh";
this.btnFresh.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.btnFresh.Size = new System.Drawing.Size(56, 23);
this.btnFresh.TabIndex = 10;
this.btnFresh.Text = "刷新";
this.btnFresh.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.btnFresh.Click += new System.EventHandler(this.btnFresh_Click);
// 
// btnExit
// 
this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnExit.Image = ((System.Drawing.Bitmap)(resources.GetObject("btnExit.Image")));
this.btnExit.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnExit.Location = new System.Drawing.Point(112, 4);
this.btnExit.Name = "btnExit";
this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.btnExit.Size = new System.Drawing.Size(56, 23);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "退出";
this.btnExit.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
// 
// lblLine
// 
this.lblLine.BackColor = System.Drawing.SystemColors.ControlText;
this.lblLine.Location = new System.Drawing.Point(5, 34);
this.lblLine.Name = "lblLine";
this.lblLine.Size = new System.Drawing.Size(367, 3);
this.lblLine.TabIndex = 12;
this.lblLine.Text = "label5";
// 
// lblSymbol
// 
this.lblSymbol.Location = new System.Drawing.Point(136, 44);
this.lblSymbol.Name = "lblSymbol";
this.lblSymbol.Size = new System.Drawing.Size(45, 16);
this.lblSymbol.TabIndex = 13;
this.lblSymbol.Text = "比較符";
// 
// cmbField0
// 
this.cmbField0.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbField0.Location = new System.Drawing.Point(48, 62);
this.cmbField0.Name = "cmbField0";
this.cmbField0.Size = new System.Drawing.Size(72, 20);
this.cmbField0.TabIndex = 14;

解决方案 »

  1.   

    //接上 // 
    // QueryForm
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
    this.ClientSize = new System.Drawing.Size(376, 133);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.cmbField0,
      this.lblSymbol,
      this.lblLine,
      this.btnExit,
      this.btnFresh,
      this.btnQuery,
      this.lblLogic,
      this.lblValue,
      this.lblField,
      this.lblIsValid,
      this.cmbLogic0,
      this.cmbSymbol0,
      this.cmbValue0,
      this.chkIsValid0});
    this.Name = "QueryForm";
    this.Text = "查詢";
    this.ResumeLayout(false); }
    #endregion private int currentGroup=0;
    private int CurrentGroup
    {
    get{return currentGroup;}
    set{currentGroup=value;}
    } private void RemoveCurrentGroup()
    {
    if (this.CurrentGroup!=0) //不允許刪除設計時建立的那組
    {
    foreach(Control ctl in this.Controls)
    {
    try
    {
    int index=int.Parse(ctl.Name.Substring(ctl.Name.Length-1,1));
    if (index==this.CurrentGroup)
    this.Controls.Remove(ctl);
    }
    catch{}
    }
    this.Height-=VerticalInterval+GroupHeight;
    this.CurrentGroup--;
    }
    } private void AddGroup()
    {
    int i=this.CurrentGroup +1;
    int top=this.chkIsValid0.Top+GroupHeight*i + VerticalInterval*i; CheckBox chkIsValid=new CheckBox();
    chkIsValid.Name="chkIsValid" + i.ToString();
    chkIsValid.Checked=true;
    chkIsValid.Size=this.chkIsValid0.Size;
    chkIsValid.Top=top;
    chkIsValid.Left=this.chkIsValid0.Left;
    chkIsValid.CheckedChanged+=new EventHandler(this.chkIsValid0_CheckedChanged); ComboBox cmbField=new ComboBox();
    cmbField.Name="cmbField" + i.ToString();
    cmbField.Size=this.cmbField0.Size;
    cmbField.Top=top;
    cmbField.Left=this.cmbField0.Left;
    cmbField.DropDownStyle=this.cmbField0.DropDownStyle;
    foreach(object o in this.cmbField0.Items)
    cmbField.Items.Add(o); ComboBox cmbSymbol=new ComboBox();
    cmbSymbol.Name="cmbSymbol" + i.ToString();
    cmbSymbol.Size=this.cmbSymbol0.Size;
    cmbSymbol.Top=top;
    cmbSymbol.Left=this.cmbSymbol0.Left;
    cmbSymbol.DropDownStyle=this.cmbSymbol0.DropDownStyle;
    foreach(object o in this.cmbSymbol0.Items)
    cmbSymbol.Items.Add(o); ComboBox cmbValue=new ComboBox();
    cmbValue.Name="cmbValue" + i.ToString();
    cmbValue.Size=this.cmbValue0.Size;
    cmbValue.Top=top;
    cmbValue.Left=this.cmbValue0.Left;
    cmbValue.DropDownStyle=this.cmbValue0.DropDownStyle;
    foreach(object o in this.cmbValue0.Items)
    cmbValue.Items.Add(o); ComboBox cmbLogic=new ComboBox();
    cmbLogic.Name="cmbLogic" + i.ToString();
    cmbLogic.Size=this.cmbLogic0.Size;
    cmbLogic.Top=top;
    cmbLogic.Left=this.cmbLogic0.Left;
    cmbLogic.DropDownStyle=this.cmbLogic0.DropDownStyle;
    cmbLogic.SelectedIndexChanged+=new EventHandler(this.cmbLogic0_SelectedIndexChanged);
    foreach(object o in this.cmbLogic0.Items)
    cmbLogic.Items.Add(o); this.Height+=VerticalInterval + GroupHeight;
    this.Controls.Add(chkIsValid);
    this.Controls.Add(cmbField);
    this.Controls.Add(cmbSymbol);
    this.Controls.Add(cmbValue);
    this.Controls.Add(cmbLogic);
    this.CurrentGroup++;
    } private void ResetGroup()
    {
    if (this.CurrentGroup!=0)
    {
    this.RemoveCurrentGroup();
    this.ResetGroup();
    }
    } private void cmbLogic0_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    ComboBox cmbLogic=(ComboBox)sender;
    string name=cmbLogic.Name;
    int index=int.Parse(name.Substring(name.Length-1,1));
    if (index==this.currentGroup )
    {
    this.AddGroup();
    foreach(Control ctl in this.Controls)
    if(ctl.Name=="chkIsValid" + index.ToString())
    ctl.Enabled=false;
    }
    } private void chkIsValid0_CheckedChanged(object sender, System.EventArgs e)
    {
    CheckBox chk=(CheckBox)sender;
    int index=int.Parse(chk.Name.Substring(chk.Name.Length-1,1))-1;
    if (index>=0)
    {
    foreach(Control ctl in this.Controls)
    if(ctl.Name=="chkIsValid" + index.ToString())
    ctl.Enabled=true;
    this.RemoveCurrentGroup();
    }
    } private void btnExit_Click(object sender, System.EventArgs e)
    {
    this.Close();
    } private void btnFresh_Click(object sender, System.EventArgs e)
    {
    this.Text=this.Controls.Count.ToString();
    } }
    }
      

  2.   

    好奇怪呀,为什么只有那两个comboBox会在foreach循环里不见了。
    我帮你顶。
      

  3.   

    我也试过了,就算全部删除也是隔一个删一个,怪了.
    开始还想是不是把那个checkbox自己删了影响到事件执行,
    但是就算把checkbox放到最后删还是剩两个.
      

  4.   

    问题出在这一句上:
    this.Controls.Remove(ctl);
    我估计是remove之后controls的次序乱掉了。
    我在foreach循环外面又加了一个for循环,让foreach多执行几遍就全删了,
    但这不是个好办法。目前还没想出什么好办法。
      

  5.   

    反正你要删除的控件只有5个,写5次foreach循环,每次判断1个control的name是否相符,逐个删除,应该行了。我没试。
      

  6.   

    这样:
    for (int i=0;i<5;i++)
    {
    foreach(Control ctl in this.Controls)
    {
    try
    {
    int index=int.Parse(ctl.Name.Substring(ctl.Name.Length-1,1));
    if (index==this.CurrentGroup)
    {
    this.Controls.Remove(ctl);
    break;
    }
    }
    catch{}
    }
    }
    测试通过。给分吧。
      

  7.   

    verystrong() 的一句 "我估计是remove之后controls的次序乱掉了。"
    讓我猛然驚醒﹐我的解決辦法是建立一個臨時的ArraryList或者Array,把
    符合條件的控件先引用住﹐最后再Remove.謝謝兩位參與。