且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将动态面板添加到Windows窗体中已存在的面板中

更新时间:2023-12-01 21:53:46

只需创建它们,并将它们添加到现有的Panel.Controls列表中:
Just create them, and add them to the existing Panel.Controls list:
Panel p1 = new Panel();
Panel p2 = new Panel();
myPanelInMainForm.Controls.Add(p1);
myPanelInMainForm.Controls.Add(p2);

您可能想要设置大小和位置,但这很简单……

You will probably want to set the size, and location, but that''s trivial...


我认为OriginalGriff"答案是完整的,但只想添加一点点转折.由于您正在使用Forms,您是否注意到..Designer.cs中有一些生成的代码?看一看该代码,您会注意到Visual Studio实际上是如何在窗体上添加控件的.您也可以在代码中使用相同的原则:)
I think OriginalGriff''s answer is complete but just want to add a little twist. Since you''re working with Forms have you noticed that you have some generated code in the ..Designer.cs? Have a look at that code and you''ll notice how Visual Studio actually adds the controls on your forms. You can use the same principles in your code also :)