C# Winforms - 将表单添加到 FlowPanel 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/638063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Winforms - Adding forms to a FlowPanel control
提问by Tom J Nowell
I have a page where I have to modify variables which are strings with pairs of values and labels. I was using a datagrid object but its not sufficient for whats required ( or eventually will not anyway ).
我有一个页面,我必须在其中修改变量,这些变量是带有值和标签对的字符串。我正在使用一个 datagrid 对象,但它不足以满足所需要的(或者最终不会)。
So I have a form which is a text label and textbox, and a flowpanel, and I'm trying to programmatically add instances of this form for each variable to the flowpanel, and Im getting nothing. Googling for the solution bring sup lots of video tutorials involving clicking on buttons in the UI designer and dropping them on flow panels, I want to do this programmatically however.
所以我有一个表单,它是一个文本标签和文本框,以及一个流程面板,我试图以编程方式为流程面板的每个变量添加此表单的实例,但我什么也没得到。谷歌搜索解决方案带来了大量视频教程,包括单击 UI 设计器中的按钮并将它们放在流面板上,但是我想以编程方式执行此操作。
What is the 'correct' or 'standard' way of doing this.
这样做的“正确”或“标准”方式是什么。
采纳答案by Marc Gravell
The data (in pairs) sounds like it might fit better with a TableLayoutPanel, but the theory is the same; just call .Controls.Add(...)and it should work:
数据(成对)听起来可能更适合 a TableLayoutPanel,但理论是相同的;只需致电.Controls.Add(...),它应该可以工作:
FlowLayoutPanel panel = new FlowLayoutPanel();
Form form = new Form();
panel.Dock = DockStyle.Fill;
form.Controls.Add(panel);
for (int i = 0; i < 100; i++)
{
panel.Controls.Add(new TextBox());
}
Application.Run(form);
or with a TableLayoutPanel:
或带有TableLayoutPanel:
TableLayoutPanel panel = new TableLayoutPanel();
Form form = new Form();
panel.Dock = DockStyle.Fill;
panel.ColumnCount = 2;
form.Controls.Add(panel);
for (int i = 0; i < 100; i++)
{
panel.Controls.Add(new Label { Text = "label " + i });
panel.Controls.Add(new TextBox { Text = "text " + i });
}
Also - I wonder if a PropertyGridwould fit your needs better? This will handle all the "get value", "show value", "parse value", "store value" logic, and can be plugged with things like ICustomTypeDescriptorto allow dynamic properties.
另外 - 我想知道 aPropertyGrid是否更适合您的需求?这将处理所有“获取值”、“显示值”、“解析值”、“存储值”逻辑,并且可以插入诸如ICustomTypeDescriptor允许动态属性之类的东西。
回答by itsmatt
To add instances of a form to a flowlayout panel, I do the following:
要将表单实例添加到 flowlayout 面板,我执行以下操作:
Form1 f1 = new Form1();
f1.TopLevel = false;
f1.Visible = true;
flowLayoutPanel1.Controls.add(f1);
Seems to work OK in my test code.
在我的测试代码中似乎工作正常。

