在表单上动态添加用户控件时,我得到了正确的输出
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
{
SqlDataReader DR1 = SqlCommand.ExecuteReader();
int y = 0;
while (DR1.Read())
{
y++;
for (int i = 0; i < y; i++)
{
UserControl2 userconrol = new UserControl2();
userconrol.Location = new Point(50, 30 * i);
userconrol.Tag = i;
userconrol.LastName = (string)DR1["LastName"];
this.Controls.Add(userconrol);
}
}
}
}
但是当我使用 flowlayoutpanel 动态添加控件时,发生了这样的事情:
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand("Select LastName from Employee", myDatabaseConnection))
using (SqlDataAdapter da = new SqlDataAdapter(SqlCommand))
{
SqlDataReader DR1 = SqlCommand.ExecuteReader();
int y = 0;
while (DR1.Read())
{
y++;
for (int i = 0; i < y; i++)
{
UserControl2 userconrol = new UserControl2();
userconrol.Tag = i;
userconrol.LastName = (string)DR1["LastName"];
flowLayoutPanel1.Controls.Add(userconrol);
}
}
}
}
问题是什么 ?我使用相同的循环为什么当我使用 flowlayoutpanel 动态添加控件时它不显示相同的输出?