TableLayout 与单元格大小的问题 (C#)

发布于 2024-10-31 08:50:38 字数 932 浏览 5 评论 0原文

我有一个最初为空的 TableLayoutPanel 控件 - 0 行和 0 列。我想形成它,并用代码中的一些控件填充它,它可以工作,但我有一些讨厌的问题 - 当我使用代码将控件添加到 TableLayoutPanel 时,列和行的大小不同(尽管所有添加的控件都是相同大小,并停靠以填充 TableLayoutPanel 容器中的单元格)。基本上第一行约占高度的 50%,接下来的两行各占 25%。

我尝试过的代码看起来像这样:

//Reseting table:
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.ColumnCount = 1;

//Size of one side of matrix:
int oneSide=(int)Math.Sqrt(mat.Matrix.Length);

tableLayoutPanel1.RowCount = oneSide;
tableLayoutPanel1.ColumnCount = oneSide;

tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / oneSide));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / oneSide));

//Matrix is an array of controls, mat is some class
tableLayoutPanel1.Controls.AddRange(mat.Matrix);

上面的代码没有完成这项工作...如何从代码中添加 somtrols,并使所有单元格大小相同? PS 我必须从代​​码中添加控件,在设计时创建行和列是不可能的。

I have a TableLayoutPanel control that is initially empty - 0 rows and 0 cols. I want to form it, and fill it with some controls from code, and it works, but i have some nasty problem - when i add controls to TableLayoutPanel using code, cols and rows are not the same size (although all added controls ARE the same size, and docked to fill cells in TableLayoutPanel container). Basically first row takes about 50% of height, and two next rows take 25% each.

code i have tried looks like this:

//Reseting table:
tableLayoutPanel1.RowCount = 1;
tableLayoutPanel1.ColumnCount = 1;

//Size of one side of matrix:
int oneSide=(int)Math.Sqrt(mat.Matrix.Length);

tableLayoutPanel1.RowCount = oneSide;
tableLayoutPanel1.ColumnCount = oneSide;

tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100 / oneSide));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / oneSide));

//Matrix is an array of controls, mat is some class
tableLayoutPanel1.Controls.AddRange(mat.Matrix);

Above code isn't doing the job...How to add somtrols from code, and make all cells the same size?
P.S. i HAVE to add controls from code, creating rows and cols in design-time is impossible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-11-07 08:50:38

手动执行此操作并不令人愉快,请让代码生成器为您完成这项工作。创建一个虚拟表单,在其上放置一个 TLP。与设计师一起按照您想要的方式进行设计。

接下来,在“解决方案资源管理器”窗口中找到该表单并打开其左侧的节点。双击 Designer.cs 文件。找到 InitializeComponent 方法中的代码并复制/粘贴它。

Doing this by hand is unpleasant, let a code generator do the job for you. Create a dummy form, drop a TLP on it. Get it the way you want it with the designer.

Next, locate the form in the Solution Explorer window and open the node to the left of it. Double-click the Designer.cs file. Locate the code in the InitializeComponent method and copy/paste it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文