Description of the problem:
Before you answer:
1) Yes the things need to be this way
2) Code sample below:
public partial class Form1 : Form
{
List<UserControl2> list;
public Form1()
{
InitializeComponent();
list = new List<UserControl2>();
for (int i = 0; i < 20; i++)
{
UserControl2 c = new UserControl2();
list.Add(c);
}
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (UserControl2 c in list)
userControl11.Controls.Add(c);
}
private void button1_Click(object sender, EventArgs e)
{
int y = 0;
foreach (UserControl2 c in list)
{
c.Location = new Point(0, y);
y += c.Height;
}
}
}
Its because Location gives the coordinates of the upper left corner of the control relative to the upper left corner of its container. So when you scroll down, the Location will change.
Here is how to fix it:
private void button1_Click(object sender, EventArgs e)
{
int y = list[0].Location.Y;
foreach (UserControl2 c in list)
{
c.Location = new Point(0, y);
y += c.Height;
}
}