Add Form to a UserControl - is this possible?

前端 未结 1 1233
春和景丽
春和景丽 2020-12-11 16:17

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 nee

相关标签:
1条回答
  • 2020-12-11 16:54

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题