C# WinForms Large offset when adding rows to TableLayoutPanel (Upd)

56 Views Asked by At

I am developing a WinForms app. I have a TableLayoutPanel which has a button that adds rows. At the start, there is one row. But when I click Add for the first time, there's a large offset before the new row. Look:

Large gap

And oh, code for the add button:

private void manual_packages_add_Click(object sender, EventArgs e) {
  static Size MulSize(Size sz, Size accord, int percent) {
    static float Percent(float n, int p) => n * (p / 100);

    SizeF origF = (SizeF)sz;
    SizeF accordF = (SizeF)accord;
    origF.Height += Percent(accordF.Height, percent);
    return origF.ToSize();
  }

  TextBox tbx = new() {
    Name = $"manual_packages_pkg{manual_nextPkgTbxIndex++}",
    Size = new((int)Math.Round(manual_packages_list.ColumnStyles[0].Width),
               (int)Math.Round(manual_packages_list.RowStyles[0].Height)),
    MaxLength = 34,
    BorderStyle = BorderStyle.Fixed3D,
  };
  Button rm = new() { Name = $"manual_packages_rm{manual_nextPkgTbxIndex}",
                      Text = "i suffer " };
  manual_packages_list.Controls.Add(tbx, 0, ++manual_packages_list.RowCount);
  manual_packages_list.Controls.Add(rm, 1, manual_packages_list.RowCount);

  // sizing
  Control? container = tbx.Parent;
  bool firstIter = true;
  while (true) {
    container = firstIter ? container : container.Parent;
    if (container == null)
      break;
    container.Size = MulSize(container.Size, tbx.Size, 100);
    firstIter = false;
  }
}

Update

The guy that was causing trouble was AutoSize set to off. I enabled it, and... there's another issue. Now buttons are off. Here's a screenshot after pressing the button (code stays unchanged):

:/

Seeking help...

0

There are 0 best solutions below