Add Form to a UserControl - is this possible?

半城伤御伤魂 提交于 2020-01-09 11:33:05

问题


Normally, controls are being added to forms. But I need to do an opposite thing - add a Form instance to container user control.

The reason behind this is that I need to embed a third-party application into my own. Converting the form to a user control is not feasible due to complexity.


回答1:


This is possible by setting the form's TopLevel property to false. Which turns it into a child window, almost indistinguishable from a UserControl. Here's a sample user control with the required code:

public partial class UserControl1 : UserControl {
    public UserControl1() {
        InitializeComponent();
    }
    public void EmbedForm(Form frm) {
        frm.TopLevel = false;
        frm.FormBorderStyle = FormBorderStyle.None;
        frm.Visible = true;
        frm.Dock = DockStyle.Fill;   // optional
        this.Controls.Add(frm);
    }
}


来源:https://stackoverflow.com/questions/7319998/add-form-to-a-usercontrol-is-this-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!