gpt4 book ai didi

c# - Winforms FlowLayoutPanel 使用自动调整控件删除不需要的空间

转载 作者:行者123 更新时间:2023-11-30 17:31:04 26 4
gpt4 key购买 nike

我有 FlowLayoutPanel

自动滚动 = 真

FlowDirection = LeftToRight

WrapContents = True

添加的控件动态地具有相同的宽度,但在高度自动调整大小。所以面板会像this , 项目之间有垂直空间。作为由控件的最大高度管理的行的高度。所以我想删除这些不需要的空格,最后的结果会像this .

如果没有办法用 FlowLayoutPanel 做到这一点,那么完美地做到这一点的正确想法是什么?

最佳答案

  1. 它是一个矩阵,应该像矩阵一样对待。
  2. 我的意见是 Panel 比这里的 FlowLayoutpanel 更合适。
  3. 请查看我的建议和输出以实现此类行为。

说明:此代码需要改进以适应所有可能的情况,但您可以从中了解如何处理此类问题的基本思路。

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
Example();
}

// space beetween controls (top and right)
public int MarginSpace = 8;
// first element location
public Point StartPoint = new Point(10, 10);
private void Example()
{
var fixesWidth = 70;
List<Label> randomLables = new List<Label>();
Random rand = new Random();
// generate lables with random heights
for (int i = 1; i < 10; i++)
{
Label lr = new Label();
var randheight = rand.Next(60, 120);
lr.Size = new Size(fixesWidth, randheight);
lr.Text = i.ToString();
lr.BackColor = Color.Black;
lr.ForeColor = Color.White;
randomLables.Add(lr);
}

// check how many elements in one "column" (possible also to add right+left margin)
var cols = panel1.Width / fixesWidth;
// create matrix object to get locations of each label
MyMatrix m = new MyMatrix(cols, randomLables.Count, 15, 70, StartPoint);
m.SetMatrix(randomLables);
int counter = 0;
// pupulate all lables with the points from MyMatrix object
foreach (Point p in m.pointsMatrix)
{
randomLables[counter].Location = p;
panel1.Controls.Add(randomLables[counter]);
counter++;
}
}
}
class MyMatrix
{
private int Rows;
private int TotalElements;
private int Cols;
private int Margin;
private int ElementWidth;
private Point StartPoint;
public MyMatrix(int cols, int totalelements, int margin, int elementwidth, Point startingpoint)
{
this.Cols = cols;
this.TotalElements = totalelements;
this.Margin = margin;
this.ElementWidth = elementwidth;
this.StartPoint = startingpoint;

// calculate number of rows
Rows = totalelements / cols;
}

public List<Point> pointsMatrix = new List<Point>();
int cellCounter = 0;
public void SetMatrix(List<Label> Labels)
{
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{

var x = StartPoint.X + j * (Margin + ElementWidth);
var y = StartPoint.Y;
if (cellCounter >= Cols)
{
// find the parallel cell in the row above
y = pointsMatrix[cellCounter - Cols].Y + Labels[cellCounter - Cols].Height + Margin;
}
else
{
// do nothing it is first row
}

Point p = new Point(x, y);
pointsMatrix.Add(p);
cellCounter += 1;
}
}
}
}

输出:
enter image description here

关于c# - Winforms FlowLayoutPanel 使用自动调整控件删除不需要的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48611995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com