在执行期间在 C# 中将面板添加到表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9401337/
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
Adding Panels to a Form in C# during execution
提问by bionicOnion
I've been diving into C# for the first time by trying to recreate a screensaver that I made in Java comprised of a grid of panels which change color randomly. So far, I've gotten the code for the panels to work, and now I'm trying to add them to a Form to prototype the layout. I plan to determine the number of panels to be displayed at runtime (so that 16:9, 4:3, 16:10, etc. can all make the screensaver look good), but the only ways that I've found from searching the internet to add elements to a Form involve using Visual Studio's design tools. Therefore, I have a few questions:
通过尝试重新创建我用 Java 制作的屏幕保护程序,我第一次涉足 C#,该屏幕保护程序由随机改变颜色的面板网格组成。到目前为止,我已经获得了面板工作的代码,现在我正在尝试将它们添加到一个表单中以对布局进行原型设计。我计划确定在运行时显示的面板数量(以便 16:9、4:3、16:10 等都可以使屏幕保护程序看起来不错),但我从搜索中找到的唯一方法互联网向表单添加元素涉及使用 Visual Studio 的设计工具。因此,我有几个问题:
How can I set up the layout of the Form in something akin to Java's GridLayout?
如何在类似于 Java 的 GridLayout 的东西中设置 Form 的布局?
What's the code needed to add elements to a Form?
将元素添加到表单所需的代码是什么?
Is there a better thing for me to be using insteadof a Form?
有没有更好的事情对我来说,使用替代表格的?
采纳答案by Reed Copsey
You can add panels to a form at runtime the same way the designer does - construct the Panel, and add it to the form (via this.Controls.Add(thePanel);).
您可以在运行时将面板添加到表单中,就像设计者所做的一样 - 构建面板,然后将其添加到表单中(通过this.Controls.Add(thePanel);)。
The easiest way to see the appropriate code is to add a panel to the form using the designer, then open up the "YourForm.designer.cs" file. The designer just generates the required code for you - but you can see the exact code required to duplicate what the designer would create.
查看适当代码的最简单方法是使用设计器向表单添加面板,然后打开“YourForm.designer.cs”文件。设计器只是为您生成所需的代码 - 但您可以看到复制设计器将创建的内容所需的确切代码。
As for the layout, I'd recommend watching the Layout Techniques for Windows Forms Developersvideo. It may give you some good clues as to the various options available for layout. There is nothing exactly like Java's GridLayout, though there are some open source projects that attempt to duplicate this functionality.
至于布局,我建议您观看Windows Forms Developers的Layout Techniques视频。它可能会为您提供有关可用于布局的各种选项的一些很好的线索。没有什么与 Java 的 GridLayout 完全相同,尽管有一些开源项目试图复制此功能。
回答by Servy
You'll want to use TableLayoutPanelor something along those lines. Then you can use the Controlsproperty to add panels to it.
你会想要使用TableLayoutPanel或类似的东西。然后您可以使用该Controls属性向其添加面板。
回答by Botonomous
You definitely are gong to need to create a form application to get this to work. In addition, every control you see in the designer, can be added programmatically.
您肯定需要创建一个表单应用程序才能使其工作。此外,您在设计器中看到的每个控件都可以通过编程方式添加。
You could have a method that cranks out new panels as needed....
您可以使用一种方法根据需要制作新面板......
Here is the code that will create a new panel:
这是将创建一个新面板的代码:
Panel panel1 = new Panel();
Once it is declared, you can access all the properties.
声明后,您可以访问所有属性。
To add the panel to the form you would do something like this....
要将面板添加到表单中,您可以执行以下操作....
myform.controls.add(panel1);
So knowing that, you can create a method that will format your panel and return or add it tor the form....
因此,知道这一点,您可以创建一个方法来格式化您的面板并将其返回或添加到表单中....

