使用滚动时如何阻止 winforms 面板更改我添加的控件的 Y 坐标?

Hwo to stop winforms panel from changing the Y coordinate of my added controls when scrolling is used?

这是我的代码:

private void btn_creategroup_Click(object sender, EventArgs e)
{
    ListView listview = new ListView();
    listview.BackColor = normalColor;
    listview.BorderStyle = BorderStyle.Fixed3D;
    listview.ForeColor = System.Drawing.Color.Black;
    listview.Name = "Group" + count2;
    int futureCount = autolayoutGroups.Controls.Count + 1;
    int actualCount = futureCount - 1;
    int spacing = 20;
    int width = (int)(currentWidth / 2) - (spacing + (spacing / 2));
    int height = (int)(currentHeight / 2) - (spacing + (spacing / 2));
    int ycount = 0;
    int xcount = 0;

    if (actualCount > 1)
    {
         if (actualCount % 2 == 0)
         {
              globalYCount += 1;
         }
    }

    if (actualCount > 1)
    {
         ycount = actualCount / 2;
    }

    if (actualCount > 0)
    {
         if (actualCount == 1)
         {
              xcount = 1;
         }
         else
         {
              if (actualCount % 2 == 0)
              {
                    xcount = 0;
              }
              else
              {
                  xcount = 1;
              }

          }
    }

    listview.Location = new System.Drawing.Point(spacing + (xcount * (width + spacing)), spacing + (ycount * (height + spacing)));
    listview.Size = new System.Drawing.Size(width, height);

    autolayoutGroups.Controls.Add(listview);
            
}

请原谅我的错误代码我是初学者,通过大量的反复试验得出了这个解决方案,当我运行我的代码在面板上启用了自动滚动并且我没有滚动时一切正常很好,我可以添加任意数量的列表视图,它们都排列得很好,但是如果滚动任何然后添加更多列表视图,新列表视图的 Y 位置将关闭。

无需滚动创建的列表视图:

向下滚动一点后创建的ListViews:

我使用消息框告诉我创建列表视图时计算出的 Y 位置是什么,然后我使用消息框和单击函数告诉我列表视图控件的实际 Y 位置,但它们没有比赛。 我该如何解决这个问题?

您必须偏移面板的滚动条位置:

listview.Location = new Point(
  spacing + (xcount * (width + spacing)),
  spacing + (ycount * (height + spacing)) + autolayoutGroups.AutoScrollPosition.Y
);