c# - 在flowlayoutpanel中动态添加控件

标签 c# winforms dynamic-controls flowlayoutpanel

在 Windows 窗体中,我可以通过这样做动态添加控件:

for (int i = 0; i < 5; i++)
{
    Button button = new Button();
    button.Location = new Point(160, 30 * i + 10);

    button.Tag = i;
    this.Controls.Add(button);
}

如何在 FlowLayoutPanel 中动态添加控件?

最佳答案

对于 FlowLayoutPanel ,您无需指定 .Location,因为控件已为您安排好:

Represents a panel that dynamically lays out its contents horizontally or vertically. ... The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. Its contents can be wrapped from one row to the next, or from one column to the next.

只需将“flowLayoutPanel1”更改为您的FlowLayoutPanel 的名称:

for (int i = 0; i < 5; i++)
{
    Button button = new Button();
    button.Tag = i;
    flowLayoutPanel1.Controls.Add(button);
}

关于c# - 在flowlayoutpanel中动态添加控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16900522/

相关文章:

c# - 显示默认设备输出的音量峰值

c# - 动态创建的 WebBrowser 不起作用

c# - 模态进度表显示 IProgress 并支持取消 WinForms 的异步任务

iOS - 在 MVVM 中动态创建控件

c# - 动态创建控件并跨回发保存控件值 - ASP.Net C#

c# - Wix 卸载整个网站而不仅仅是应用程序

c# - 使用领域驱动设计时应用存储在数据库中的业务规则

c# - 如何在 WPF 中制作右键单击按钮上下文菜单?

c# - 为什么会在不同时间调用 LostFocus 事件?

ASP.NET/C# 对动态创建控件的混淆