请看如下代码,我在panel上动态生成了一组控件,有TextBox、Combobox,现在要获取控件的值,但是断点设到if里面,可是断点走不到if里面,也就是说判断条件不成立,问题在哪呢?求救各位达人double [] StxtLength = new double[10];
ComboBox[] ScbBoxLoadMaterial=new ComboBox[10];foreach (Control c in this.panlSubsection.Controls)
{     if (c is TextBox && ((TextBox)c).Name.Contains("txtLength"+i))
     {
        TextBox s = ((TextBox)c);
        StxtLength[i] = double.Parse (s.Text.ToString());
     }
     if (c is ComboBox && ((ComboBox)c).Name.Contains("cbBoxLoadMaterial" + i))
     {
        ComboBox s=((ComboBox)c);
        ScbBoxLoadMaterial[i]=s;
     }
     .......
}

解决方案 »

  1.   

    既然知道是判断条件不成立,就分析判断的条件了,如是是textbox,那么就是名字中不含你写的那些东西喽!
      

  2.   

    可是名字中是包含的,我另外有个方法是用来定义动态生成的控件的private void cbBoxPartitionSum_SelectedIndexChanged(object sender, EventArgs e)
            {
              int n;//外形所分段数
                n = Convert.ToInt32(this.cbBoxPartitionSum.SelectedIndex);            panlSubsection.Controls.Clear();//放置动态控件的容器
                //panlSubsection.Controls.Add(tableLayoutPanel7);
                for (int i = 0; i <= n; i++)
                {
                    //动态建立“外形”标签
                    Label lbl = new Label();
                    lbl.Text = "段" + (i + 1);
                    lbl.Name = "lblSubsection" + i;
                    lbl.Location = new Point(18, 50+50 * i);
                    lbl.Size = new Size(29, 21);                //动态建立“外形”文本框及下拉列表
                    TextBox txt1 = new TextBox();
                    txt1.Text = "130";
                    txt1.Name = "txtLength" + i;
                    txt1.Location = new Point(60, 50 + 50 * i);
                    txt1.Size = new Size(30, 21);                TextBox txt2 = new TextBox();
                    txt2.Text = "0";
                    txt2.Name = "txtHeight" + i;
                    txt2.Location = new Point(120, 50 + 50 * i);
                    txt2.Size = new Size(30, 21);                TextBox txt3 = new TextBox();
                    txt3.Text = "0";
                    txt3.Name = "txtInclineAngle" + i;
                    txt3.Location = new Point(180, 50 + 50 * i);
                    txt3.Size = new Size(30, 21);                ComboBox cbBox = new ComboBox();
                    cbBox.Text = "否";
                    cbBox.Name = "cbBoxLoadMaterial" + i;
                    cbBox.Location = new Point(231, 50 + 50 * i);
                    cbBox.Size = new Size(40, 20);
                    cbBox.Items.AddRange(new object[]{
                    "是",
                    "否"});                TextBox txt4 = new TextBox();
                    txt4.Text = "0";
                    txt4.Name = "txtDriveRollerNumber" + i;
                    txt4.Location = new Point(308, 50 + 50 * i);
                    txt4.Size = new Size(30, 21);                TextBox txt5 = new TextBox();
                    txt5.Text = "0";
                    txt5.Name = "txtRR180EndNumber" + i;
                    txt5.Location = new Point(390, 50 + 50 * i);
                    txt5.Size = new Size(30, 21);                TextBox txt6 = new TextBox();
                    txt6.Text = "0";
                    txt6.Name = "txtRR180MiddleNumber" + i;
                    txt6.Location = new Point(458, 50 + 50 * i);
                    txt6.Size = new Size(30, 21);                TextBox txt7 = new TextBox();
                    txt7.Text = "0";
                    txt7.Name = "txtRR180HeadNumber" + i;
                    txt7.Location = new Point(523, 50 + 50 * i);
                    txt7.Size = new Size(30, 21);                TextBox txt8 = new TextBox();
                    txt8.Text = "0";
                    txt8.Name = "txtRR90Number" + i;
                    txt8.Location = new Point(588, 50 + 50 * i);
                    txt8.Size = new Size(30, 21);                TextBox txt9 = new TextBox();
                    txt9.Text = "0";
                    txt9.Name = "txtRR45Number" + i;
                    txt9.Location = new Point(653, 50 + 50 * i);
                    txt9.Size = new Size(30, 21);                TextBox txt10 = new TextBox();
                    txt10.Text = "0";
                    txt10.Name = "txtDriPowLoad" + i;
                    txt10.Location = new Point(706, 50 + 50 * i);
                    txt10.Size = new Size(40, 21);                TextBox txt11= new TextBox();
                    txt11.Text = "0";
                    txt11.Name = "txtDriPowBack" + i;
                    txt11.Location = new Point(761, 50 + 50 * i);
                    txt11.Size = new Size(40, 21);                panlSubsection.Controls.Add(txt1);
                    panlSubsection.Controls.Add(txt2); 
                    panlSubsection.Controls.Add(txt3);
                    panlSubsection.Controls.Add(txt4);
                    panlSubsection.Controls.Add(txt5);
                    panlSubsection.Controls.Add(txt6);
                    panlSubsection.Controls.Add(txt7);
                    panlSubsection.Controls.Add(txt8);
                    panlSubsection.Controls.Add(txt9);
                    panlSubsection.Controls.Add(txt10);
                    panlSubsection.Controls.Add(txt11);
                    panlSubsection.Controls.Add(lbl);
                    panlSubsection.Controls.Add(cbBox);
                }
            }
      

  3.   

        if (c is TextBox && ((TextBox)c).Name.Contains("txtLength"+i))
    这个i赋了什么值?会不会越界?
      

  4.   

    完整的取值代码是这样的://获取外形里面板动态控件的值
                 int i = 0;
                double [] StxtLength = new double[10];
                double[] StxtHeight = new double[10];
                double[] StxtInclineAngle = new double[10];            ComboBox[] ScbBoxLoadMaterial=new ComboBox[10];            int[] StxtDriveRollerNumber = new int[10];
                int[] StxtRR180EndNumber = new int[10];
                int[] StxtRR180MiddleNumber = new int[10];
                int[] StxtRR180HeadNumber = new int[10];
                int[] StxtRR90Number = new int[10];
                int[] StxtRR45Number = new int[10];
                Double [] StxtDriPowLoad = new Double[10];
                Double [] StxtDriPowBack = new Double[10];            foreach (Control c in this.panlSubsection.Controls)
                {                if (c is TextBox && ((TextBox)c).Name.Contains("txtLength"+i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtLength[i] = double.Parse (s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtHeight" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtHeight[i] =double.Parse(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtInclineAngle" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtInclineAngle[i] = double.Parse (s.Text.ToString());
                    }
                    if (c is ComboBox && ((ComboBox)c).Name.Contains("cbBoxLoadMaterial" + i))
                    {
                        ComboBox s=((ComboBox)c);
                        ScbBoxLoadMaterial[i]=s;
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtDriveRollerNumber" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtDriveRollerNumber[i] =Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtRR180EndNumber" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtRR180EndNumber[i] = Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtRR180MiddleNumber" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtRR180MiddleNumber[i] = Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtRR180HeadNumber" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtRR180HeadNumber[i] = Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtRR90Number" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtRR90Number[i] = Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtRR45Number" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtRR45Number[i] = Convert.ToInt16(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtDriPowLoad" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtDriPowLoad[i] = Double.Parse(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtDriPowBack" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtDriPowBack[i] =Double.Parse(s.Text.ToString()) ;
                    }
                    i++;
                    if (i > 9) break;
                }
      

  5.   

    我把i改成了O,用你的代码调了一下,没问题呀?
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int n;//外形所分段数
                n = Convert.ToInt32(4);            panlSubsection.Controls.Clear();//放置动态控件的容器
                //panlSubsection.Controls.Add(tableLayoutPanel7);
                for (int i = 0; i <= n; i++)
                {
                    //动态建立“外形”标签
                    Label lbl = new Label();
                    lbl.Text = "段" + (i + 1);
                    lbl.Name = "lblSubsection" + i;
                    lbl.Location = new Point(18, 50 + 50 * i);
                    lbl.Size = new Size(29, 21);                //动态建立“外形”文本框及下拉列表
                    TextBox txt1 = new TextBox();
                    txt1.Text = "130";
                    txt1.Name = "txtLength" + i;
                    txt1.Location = new Point(60, 50 + 50 * i);
                    txt1.Size = new Size(30, 21);                TextBox txt2 = new TextBox();
                    txt2.Text = "0";
                    txt2.Name = "txtHeight" + i;
                    txt2.Location = new Point(120, 50 + 50 * i);
                    txt2.Size = new Size(30, 21);                TextBox txt3 = new TextBox();
                    txt3.Text = "0";
                    txt3.Name = "txtInclineAngle" + i;
                    txt3.Location = new Point(180, 50 + 50 * i);
                    txt3.Size = new Size(30, 21);                ComboBox cbBox = new ComboBox();
                    cbBox.Text = "否";
                    cbBox.Name = "cbBoxLoadMaterial" + i;
                    cbBox.Location = new Point(231, 50 + 50 * i);
                    cbBox.Size = new Size(40, 20);
                    cbBox.Items.AddRange(new object[]{
                    "是",
                    "否"});                TextBox txt4 = new TextBox();
                    txt4.Text = "0";
                    txt4.Name = "txtDriveRollerNumber" + i;
                    txt4.Location = new Point(308, 50 + 50 * i);
                    txt4.Size = new Size(30, 21);                TextBox txt5 = new TextBox();
                    txt5.Text = "0";
                    txt5.Name = "txtRR180EndNumber" + i;
                    txt5.Location = new Point(390, 50 + 50 * i);
                    txt5.Size = new Size(30, 21);                TextBox txt6 = new TextBox();
                    txt6.Text = "0";
                    txt6.Name = "txtRR180MiddleNumber" + i;
                    txt6.Location = new Point(458, 50 + 50 * i);
                    txt6.Size = new Size(30, 21);                TextBox txt7 = new TextBox();
                    txt7.Text = "0";
                    txt7.Name = "txtRR180HeadNumber" + i;
                    txt7.Location = new Point(523, 50 + 50 * i);
                    txt7.Size = new Size(30, 21);                TextBox txt8 = new TextBox();
                    txt8.Text = "0";
                    txt8.Name = "txtRR90Number" + i;
                    txt8.Location = new Point(588, 50 + 50 * i);
                    txt8.Size = new Size(30, 21);                TextBox txt9 = new TextBox();
                    txt9.Text = "0";
                    txt9.Name = "txtRR45Number" + i;
                    txt9.Location = new Point(653, 50 + 50 * i);
                    txt9.Size = new Size(30, 21);                TextBox txt10 = new TextBox();
                    txt10.Text = "0";
                    txt10.Name = "txtDriPowLoad" + i;
                    txt10.Location = new Point(706, 50 + 50 * i);
                    txt10.Size = new Size(40, 21);                TextBox txt11 = new TextBox();
                    txt11.Text = "0";
                    txt11.Name = "txtDriPowBack" + i;
                    txt11.Location = new Point(761, 50 + 50 * i);
                    txt11.Size = new Size(40, 21);                panlSubsection.Controls.Add(txt1);
                    panlSubsection.Controls.Add(txt2);
                    panlSubsection.Controls.Add(txt3);
                    panlSubsection.Controls.Add(txt4);
                    panlSubsection.Controls.Add(txt5);
                    panlSubsection.Controls.Add(txt6);
                    panlSubsection.Controls.Add(txt7);
                    panlSubsection.Controls.Add(txt8);
                    panlSubsection.Controls.Add(txt9);
                    panlSubsection.Controls.Add(txt10);
                    panlSubsection.Controls.Add(txt11);
                    panlSubsection.Controls.Add(lbl);
                    panlSubsection.Controls.Add(cbBox);
                }
            }        private void button2_Click(object sender, EventArgs e)
            {
                double [] StxtLength = new double[10];
    ComboBox[] ScbBoxLoadMaterial=new ComboBox[10];foreach (Control c in this.panlSubsection.Controls)
    {     if (c is TextBox && ((TextBox)c).Name.Contains("txtLength"+0))
         {
            TextBox s = ((TextBox)c);
            //StxtLength[i] = double.Parse (s.Text.ToString());
         }
         if (c is ComboBox && ((ComboBox)c).Name.Contains("cbBoxLoadMaterial" + 1))
         {
            ComboBox s=((ComboBox)c);
            //ScbBoxLoadMaterial[i]=s;
         }
     }
            }
        }
    }
      

  6.   

    为什么不是+i.ToString()?
    能编译过去?
      

  7.   

    你跟一下吧,应该是你的i 的值不对,我把i改成0,因为我明确知道有个txtLength0,所以改成O试了一下没有问题的。
      

  8.   

    我调试了一下,第一个if可以进去,但是第二个、第三个if不进去了,不知道是不是控件c的问题?
      

  9.   

    楼主应该把逻辑改一下:if (c is TextBox )
    {
     TextBox s = ((TextBox)c);
     if(((TextBox)c).Name.Contains("txtLength"+i))
     {    StxtLength[i] = double.Parse (s.Text.ToString());
     }
     else if()
     {
     }
    }
      

  10.   

    怎么不对呢?还可以再改进:
    if (c is TextBox )
    {
     TextBox s = ((TextBox)c);
     string text=s.Text;
     if(s.Name.Contains("txtLength"+i))
     {    StxtLength[i] = double.Parse (text);
     }
     else if()
     {
     }
    }
      

  11.   

    现在的问题是要求第一次循环的时候,txtLength0,txtHeight0,txtInclineAngle0.....都要能够取值,也就是这n多个并列的if语句中的动态控件都能够取到值,第二次循环的时候txtLength1,txtHeight1,txtInclineAngle1.....也能取到第二组值,以此类推。但是现在只能判断到第一个if语句,第二个、第三个if条件就不成立了,不知道为什么?if (c is TextBox && ((TextBox)c).Name.Contains("txtLength"+i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtLength[i] = double.Parse (s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtHeight" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtHeight[i] =double.Parse(s.Text.ToString());
                    }
                    if (c is TextBox && ((TextBox)c).Name.Contains("txtInclineAngle" + i))
                    {
                        TextBox s = ((TextBox)c);
                        StxtInclineAngle[i] = double.Parse (s.Text.ToString());
                    }
                    if (c is ComboBox && ((ComboBox)c).Name.Contains("cbBoxLoadMaterial" + i))
                    {
                        ComboBox s=((ComboBox)c);
                        ScbBoxLoadMaterial[i]=s;
                    }
                    ......
      

  12.   

    你的逻辑关系还是没搞清啊。。
    再加一句代码,估计你就明白了:(一个控件的Name同时会包含txtLength、txtHeight等吗?)if (c is TextBox )
    {
     TextBox s = ((TextBox)c);
     string text=s.Text;
     string name=s.Name;//调试时留意下这个值的变化。
     if(name.Contains("txtLength"+i))
     {    StxtLength[i] = double.Parse (text);
     }
     else if(name.Contains("txtHeight"+i))
     {
     }
    ......
    }
    eles if (c is ComboBox)
    {
    ......
    }睡觉去喽