我在一个方法中有一个while循环,该方法应该创建24个动态创建的对象。然而,循环创建3然后停止。
int test = 24;
while (placenum< test)
{
FlowLayoutPanel name = new FlowLayoutPanel();
FlowLayoutPanel data = new FlowLayoutPanel();
FlowLayoutPanel pic = new FlowLayoutPanel();
int sort = 0;
while (sort == 0)
{
SearchR.Searchrdata.Controls.Add(first);
first.Margin = new Padding(50, 50, 3, 50);
first.Size = new Size(320, 320);
first.BackColor = Color.Azure;
first.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
first.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
first.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 1;
placenum = placenum + 1;
}
while (sort == 1)
{
SearchR.Searchrdata.Controls.Add(two);
two.Margin = new Padding(0, 50, 3, 50);
two.Size = new Size(320, 320);
two.BackColor = Color.DarkGray;
two.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
two.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
two.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 2;
placenum = placenum + 1;
}
while (sort == 2)
{
SearchR.Searchrdata.Controls.Add(last);
last.Margin = new Padding(0, 50, 10, 50);
last.Size = new Size(320, 320);
last.BackColor = Color.Bisque;
last.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
last.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
last.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 0;
placenum = placenum + 1;
}
};
return null;
答案 0 :(得分:2)
从外部;
循环的末尾删除while
。它会在1次迭代后导致循环终止。 P.S它将创建3x24 = 72
个总对象而不是24个。
更好的解决方案是:
for( int i = 0; i < 8; i++)
{
FlowLayoutPanel name = new FlowLayoutPanel();
FlowLayoutPanel data = new FlowLayoutPanel();
FlowLayoutPanel pic = new FlowLayoutPanel();
SearchR.Searchrdata.Controls.Add(first);
first.Margin = new Padding(50, 50, 3, 50);
first.Size = new Size(320, 320);
first.BackColor = Color.Azure;
first.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
first.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
first.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
SearchR.Searchrdata.Controls.Add(two);
two.Margin = new Padding(0, 50, 3, 50);
two.Size = new Size(320, 320);
two.BackColor = Color.DarkGray;
two.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
two.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
two.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
SearchR.Searchrdata.Controls.Add(last);
last.Margin = new Padding(0, 50, 10, 50);
last.Size = new Size(320, 320);
last.BackColor = Color.Bisque;
last.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
last.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
last.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
}
}
return null;
答案 1 :(得分:1)
您有三个名为first
,two
和last
的变量。据推测,这些是您尝试添加的控件。问题是您是一遍又一遍地添加三个变量的相同实例。如果您希望每次添加新的实例,则每次都应创建first
,two
和last
的新实例。请注意,您已使用name
,data
和pic
执行此操作。
另外,正如其他人所说,你也不需要while (sort == 1)
它应该是if (sort == 1)
。
最后,请注意,如果您通过调试器运行代码并逐步执行,您会看到循环运行的次数是您想要的,并且您可能已经弄清楚真正的问题是什么。