c# - 将每个下方的按钮设置为面板
问题描述
我正在创建一个与 WhatsApp Web 非常相似的表单,但使用的是 Windows 表单。所以我想实现这个:
我创建了一个面板,在该面板内有一个按钮,如下所示:
var button = new Button();
button.Text = "test customer";
button.Font = new Font("Arial",12, FontStyle.Bold) ;
button.TextAlign = ContentAlignment.MiddleRight;
button.Image = imageList1.Images[0];
button.ImageAlign = ContentAlignment.MiddleLeft;
button.Width = 293;
button.Height = 50;
var button2 = new Button();
button2.Text = "test customer 2";
button2.Font = new Font("Arial",12, FontStyle.Bold) ;
button2.TextAlign = ContentAlignment.MiddleRight;
button2.Image = imageList1.Images[0];
button2.ImageAlign = ContentAlignment.MiddleLeft;
button2.Width = 293;
button2.Height = 50;
pnlCustomers.Controls.Add(button);
pnlCustomers.Controls.Add(button2);
这仅用于测试目的。foreach
我将在一个语句中创建多个按钮,可能是 100 个或更多。我的问题是,如何将每个按钮对齐在另一个下方
更新
我尝试将其TableLayotPanel
用作下面的评论,但发生了一些奇怪的事情,一个按钮位于顶部,另一个位于底部TableLayoutPanel
,我该如何解决?
代码:
var button = new Button();
button.Text = "test customer";
button.Font = new Font("Arial",12, FontStyle.Bold) ;
button.TextAlign = ContentAlignment.MiddleRight;
button.Image = imageList1.Images[0];
button.ImageAlign = ContentAlignment.MiddleLeft;
button.Width = 293;
button.Height = 50;
var button2 = new Button();
button2.Text = "test customer 2";
button2.Font = new Font("Arial", 12, FontStyle.Bold);
button2.TextAlign = ContentAlignment.MiddleRight;
button2.Image = imageList1.Images[0];
button2.ImageAlign = ContentAlignment.MiddleLeft;
button2.Width = 293;
button2.Height = 50;
tlpCustomers.RowCount = 2;
tlpCustomers.AutoSize = true;
tlpCustomers.Controls.Add(button,0,0);
tlpCustomers.Controls.Add(button2,0,1);
解决方案
首先,我按照@Dmitry 的建议使用 TableLayoutPanel。
一旦我这样做了,我发现了一个问题,一个按钮进入面板顶部,另一个按钮位于底部,这是因为每一行都有一个预定义的Height
值,所以它是total of the panel height / bottom count
,为了解决我只需将面板的高度值更改为:
TableLayoutRowStyleCollection styles =
tlpCustomers.RowStyles;
foreach (RowStyle style in styles)
{
style.SizeType = SizeType.Absolute;
style.Height = 50;
}
它有效:
所有代码:
var button = new Button();
button.Text = "test customer";
button.Font = new Font("Arial",12, FontStyle.Bold) ;
button.TextAlign = ContentAlignment.MiddleRight;
button.Image = imageList1.Images[0];
button.ImageAlign = ContentAlignment.MiddleLeft;
button.Width = 293;
button.Height = 50;
var button2 = new Button();
button2.Text = "test customer 2";
button2.Font = new Font("Arial", 12, FontStyle.Bold);
button2.TextAlign = ContentAlignment.MiddleRight;
button2.Image = imageList1.Images[0];
button2.ImageAlign = ContentAlignment.MiddleLeft;
button2.Width = 293;
button2.Height = 50;
tlpCustomers.RowCount = 2;
TableLayoutRowStyleCollection styles =
tlpCustomers.RowStyles;
foreach (RowStyle style in styles)
{
style.SizeType = SizeType.Absolute;
style.Height = 50;
}
tlpCustomers.AutoSize = true;
tlpCustomers.Controls.Add(button,0,0);
tlpCustomers.Controls.Add(button2,0,1);
如果有更准确的解决方案,我非常感谢您可以与社区分享。
推荐阅读
- html - HTML 输出加载和显示滚动条的时间过长
- magento - 在页面加载时获取数据库查询日志
- react-native - 如何使用 eslint 设置内联样式规则?
- discord.py - discord.py 如何使用 client.login 而不是 client.run
- r - 如何使列中的值成为具有相应值的单独列?
- python - python, mongo and marshmallow: datetime struggles
- sql - SQL Server 客户端字符串到(强制)整数转换问题
- python - 如何将我的模拟刷新到开始?
- android - 为什么模拟器中没有显示格式化文本
- c++ - 我们不能在 C++ 中声明模板成员吗?