Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Here is my code:

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);
            
}

please excuse my bad code I’m a beginner and arrived at this solution through lots of trial and error, when I run my code with autoscroll enabled on the panel and I don’t scroll everything works fine, I can add as many listviews as I want and they all lineup fine, but if a scroll any and then add more listviews the Y position of the new list views is off.

List views created without scrolling:
List views created without scrolling

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ListViews created after scrolling down a little:
enter image description here

I used message boxes to tell me what the calculated Y position was when I created the listviews and then I used a message box and a click function to tell me the actual Y position of the listview control and they don’t match.
How can I fix this?

>Solution :

You would have to offset the scrollbar position of the panel:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading