简体   繁体   English

我们可以在C#中使用来自另一个winform的winform的面板吗?

[英]Can we use a panel of some winform from another winform in C#?

I want to use a win-form in C# and display its contents or the whole form in a panel used in another form. 我想在C#中使用win-form,并在另一个表单中使用的面板中显示其内容或整个表单。

Lets say i have a form A with a panel Panel_of_A and a form B with a panel Panel_of_B 假设我有一个带有面板Panel_of_A的表格A和一个带有面板Panel_of_B的表格B.

I want to display contents of Panel_of_A into Panel_of_B or you may say i want to use Panel_of_A in another form. 我想将Panel_of_A的内容显示在Panel_of_B中,或者您可能会说我想以另一种形式使用Panel_of_A。

Or you may provide help for displaying a form say A into a panel on form B. 或者,您可以提供帮助,以便在表格B的面板中显示表格A。

Hope i made u understand my thoughts... 希望我让你理解我的想法......

I think a better way for doing this is to create a User Control which contains that panel and (re)use it on both forms. 我认为更好的方法是创建一个包含该面板的用户控件,并在两个表单上重新使用它。

Here is a tutorial how to create User Controls on MSDN . 这是一个如何在MSDN上创建用户控件教程

创建一个包含要复制的逻辑的用户控件,然后在两个位置包含新的用户控件。

How's this for displaying an instance of FormA inside a panel on FormB? 如何在FormB的面板中显示FormA的实例? The UserControl is probably the better way to go, but what you asked for is possible. UserControl可能是更好的方法,但你要求的是可能的。

fa = new FormA();
fa.TopLevel = false;
fa.FormBorderStyle = FormBorderStyle.None;
fa.Dock = DockStyle.Fill; 

fb = new FormB();
fb.panel1.Controls.Add(fa); // if panel1 was public

fa.Show();
fb.Show();

I would suggest defining the panel you want to reuse as a separate class, and place this on both forms. 我建议将要重用的面板定义为单独的类,并将其放在两个表单上。 If you need the displayed data to be the same as well, bind to a business object than can be passed between the forms. 如果您需要显示的数据也相同,则绑定到可以在表单之间传递的业务对象。

Depending on the size and scale of your application, you may want to consider the (free) Composite UI Application Block 根据应用程序的大小和规模,您可能需要考虑(免费) Composite UI Application Block

From the blurb: 从模糊:

It provides proven practices to build complex smart client user interfaces based on well known design patterns such as the Composite pattern, in which simple user interface parts can be combined to create complex solutions, but at the same time allowing these parts to be independently developed, tested, and deployed. 它提供了基于众所周知的设计模式(如Composite模式)构建复杂智能客户端用户界面的成熟实践,其中简单的用户界面部件可以组合以创建复杂的解决方案,但同时允许这些部件独立开发,测试和部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM