保持面板之间的距离相同 [英] Keeping distance between panels the same

查看:139
本文介绍了保持面板之间的距离相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows窗体应用程序中有几个面板,它们分为两列,最多4行,因此最多8个面板.每个单个面板中包含的元素数量会在运行时发生变化,因此不要浪费在监视器上,我将所有元素设置为自动调整大小.问题是,我不知道如何正确放置它们,例如当第一个收缩时如何使其他三个稍微抬高一点,以便它们之间没有太多空间.

I have several panels in a windows form application, they are sorted in two columns and maximally 4 rows, so maximally 8 panels. The number of elements included in every single panel changes during the runtime, so not to waste place on monitor I set all of them to autosize. The problem is, I dont know how I can keep them placed correctly, like how to make that when the first one shrinks that the other three come up a bit so there is not too much space between them.

推荐答案

尝试使用TableLayoutPanelFlowLayoutPanel(甚至可能是SplitContainer).它们对于此类任务都非常有用.您可以在工具箱的Containers部分中找到它们.您可以通过适当设置面板的边距来保持正确的距离. TableLayoutPanel为您提供用于调整行和列大小的不同选项(绝对或百分比大小或自动).同样,通过使用面板和控件的DockAnchor属性,可以在调整大小或添加和删除控件时获得动态行为.

Try to use the TableLayoutPanel or the FlowLayoutPanel (or possibly even a SplitContainer). They all can be very useful for this kind of task. You find them in the section Containers in the Toolbox. You can keep the right distance by setting the margins of the panels appropriately. The TableLayoutPanel gives you different options for sizing the rows and columns (absolute or percent size or auto). Also by working with the Dock or the Anchor properties of the panels and controls you can attain a dynamic behavior when resizing or adding and removing controls.

您可能还必须设置控件的MinimumSizeMaximumSize属性.

You might also have to set the MinimumSize and MaximumSize properties of the controls.

您可以在TableLayoutPanel

int count = tableLayoutPanel1.Controls.Count;
int newColumn = count % 2;
int newRow = count / 2;
if (newRow >= tableLayoutPanel1.RowCount) {
    tableLayoutPanel1.RowCount++;

    // Set appropriate row style
    tableLayoutPanel1.RowStyles.Add(new RowStyle { SizeType = SizeType.AutoSize });
}
var newControl = new Button { Dock = DockStyle.Fill };
tableLayoutPanel1.Controls.Add(newControl, newColumn, newRow);

这篇关于保持面板之间的距离相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆