简体   繁体   中英

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.

Lets say i have a form A with a panel Panel_of_A and a form B with a panel Panel_of_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.

Or you may provide help for displaying a form say A into a panel on form B.

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 .

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

How's this for displaying an instance of FormA inside a panel on FormB? The UserControl is probably the better way to go, but what you asked for is possible.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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