简体   繁体   中英

Can you load a .Net form as a control?

I want to load a desktop application, via reflection, as a Control inside another application.

The application I'm reflecting is a legacy one - I can't make changes to it.

I can dynamically access the Form, but can't load it as a Control.

In .Net Form expands on Control, and I can assign the reflected Form as a Control, but it throws a run-time exception.

Forms cannot be loaded as controls.

Is there any way to convert the form to a control?

Yes, this works just fine. I'm working on a .NET app right now that loads forms into a panel on a host form.

The relevant snippet:

// setup the new form
form.TopLevel = false;
form.FormBorderStyle = FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Show ( );

// add to the panel's list of child controls
panelFormHost.Controls.Add ( form );

You should be able to add the form to the controls collection of your parent form...

See here: http://vbcity.com/forums/topic.asp?tid=30539

If that fails, try using the adapter pattern to create a container with your legacy form inside it, then load it in an MDI maybe?

What is the exception you get? Is it possible that the control itself is giving the exception (vs the framework)? Perhaps something is called in the original applications Main function that is not being called?

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