Home > Net >  Hwo to stop winforms panel from changing the Y coordinate of my added controls when scrolling is use
Hwo to stop winforms panel from changing the Y coordinate of my added controls when scrolling is use

Time:11-26

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

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?

CodePudding user response:

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
);
  • Related