请看如下代码,我在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;
}
.......
}
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;
}
.......
}
{
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);
}
}
这个i赋了什么值?会不会越界?
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;
}
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;
}
}
}
}
}
能编译过去?
{
TextBox s = ((TextBox)c);
if(((TextBox)c).Name.Contains("txtLength"+i))
{ StxtLength[i] = double.Parse (s.Text.ToString());
}
else if()
{
}
}
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()
{
}
}
{
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;
}
......
再加一句代码,估计你就明白了:(一个控件的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)
{
......
}睡觉去喽