1
public static object loadForm(Form formToLoad, TabControl homeTabControl)
{
    //Check if formToLoad parameter is NULL
    if (formToLoad == null) throw new ArgumentNullException("formToLoad");

    //get the parent/ownining form
    Form form1 = new Form1();

    //set formToLoad properties
    formToLoad = new Form
    {
        Owner = form1,
        FormBorderStyle = FormBorderStyle.None,
        TopLevel = false,
        Dock = DockStyle.Fill
    };

    //add formToLoad to tabControl tabPage
    homeTabControl.TabPages["tabPageHome"].Controls.Add(formToLoad);
    formToLoad.Show();

    return formToLoad;
}

当我通过单击按钮调用我的代码时,为什么 formToLoad 不显示在 tabControl 页面中?

private void button3_Click(object sender, EventArgs e)
{
    LeaveMainForm lM = new LeaveMainForm();
    AppCode.FormLoader.loadForm(lM, homeTabControl);
}
4

1 回答 1

2

您正在使用新实例覆盖您尝试加载的实际表单Form,在此行中:

formToLoad  = new Form

尝试这个 :

//set formToLoad properties
formToLoad.Owner = form1;
formToLoad.FormBorderStyle = FormBorderStyle.None;
formToLoad.TopLevel = false; 
formToLoad.Dock = DockStyle.Fill;
于 2013-02-07T07:59:26.927 回答