从拆分器面板中删除后,无法显示Winform

时间:2018-10-26 04:34:48

标签: c# winforms splitterpanel

我有2种形式(Form1和Form2)。 Form1有一个带有两个面板的拆分器。我在拆分器控件的panel2中添加了Form2。我想弹出而不弹出Form2而不创建Form2的新实例。请在下面找到代码段:

public partial class Form1 : Form
{
 private Form2 form2 = null;
 public Form1()
 {
     InitializeComponent();
 }
 private void Form1_Load(object sender, EventArgs e)
 {
        form2 = new Form2();
        form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        form2.Dock = DockStyle.Fill;
        form2.TopLevel = false;
        splitContainer1.Panel2.Controls.Add(form2);
        form2.Pop += new EventHandler(PopForm);
        form2.Show();
 }
 //button click event handler from Form2
 private void PopForm(object sender, EventArgs e)
 {
     Button b = sender as Button;
     if(b.Text.ToUpper() == "POPOUT")
     {
         splitContainer1.Panel2Collapsed = true;
         splitContainer1.Panel2.Controls.Remove(form2);
         //need to show the form without creating a new instance to maintain state
         form2 = new Form2();
         form2.SelectedMailId = 1;
         form2.Pop += new EventHandler(PopForm);
         form2.SetButtonText = "PopIn";
         form2.Show();                
     }
     else
     {
         //this works fine
         splitContainer1.Panel2Collapsed = false;
         form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
         form2.Dock = DockStyle.Fill;
         form2.TopLevel = false;
         splitContainer1.Panel2.Controls.Add(form2);
     }
   }
 }

弹出窗口时,如何在不创建新实例的情况下显示Form2?

1 个答案:

答案 0 :(得分:1)

弹出菜单设置边框样式和顶级样式

 if(b.Text.ToUpper() == "POPOUT")
 {
     splitContainer1.Panel2Collapsed = true;
     splitContainer1.Panel2.Controls.Remove(form2);
     //need to show the form without creating a new instance to maintain state
     form2.TopLevel = true;
     form2.FormBorderStyle = FormBorderStyle.Sizable;
     // setup your settings
     form2.Show();                
 }