c#表单应用程序不关闭面板的用户控件。

时间:2022-01-21 15:49:51

I have created one main form(MainImagincup) with two panels(panel,panel2). And an usercontrol to show in the panel2. I have used this code to show :

我已经创建了一个主要的表单(MainImagincup),有两个面板(panel,panel2)。并在panel2中显示用户控件。我用这段代码来说明:

Number_Eng_Ban jk = new Number_Eng_Ban();

Number_Eng_Ban jk = new Number_Eng_Ban();

        jk.Show();
        panel2.Controls.Add(jk); 

And this code to remove:

要删除的代码:

            MainImaginCp pk = new MainImaginCp();
            Number_Eng_Ban jk = new Number_Eng_Ban();
            pk.panel2.Controls.Clear();
            pk.panel2.Controls.Remove(jk);

But it is not removing from the main form . What I can do ?

但它并没有从主要形式中移除。我能做什么?

1 个解决方案

#1


1  

jk is well-referenced when you call pk.panel2.Controls.Remove(jk);?
It seems to me that jk is cleared when you exit method in which you add it to the panel.
You should try to declare Number_Eng_Ban jk; in your class, then (when you want to add it) do jk = new Number_Eng_Ban();

当你调用pk.panel2.Controls.Remove(jk)时,jk是很好的引用。在我看来,当您将方法添加到面板时,jk将被清除。您应该尝试声明Number_Eng_Ban jk;在您的类中,然后(当您想添加它时)执行jk = new Number_Eng_Ban();

#1


1  

jk is well-referenced when you call pk.panel2.Controls.Remove(jk);?
It seems to me that jk is cleared when you exit method in which you add it to the panel.
You should try to declare Number_Eng_Ban jk; in your class, then (when you want to add it) do jk = new Number_Eng_Ban();

当你调用pk.panel2.Controls.Remove(jk)时,jk是很好的引用。在我看来,当您将方法添加到面板时,jk将被清除。您应该尝试声明Number_Eng_Ban jk;在您的类中,然后(当您想添加它时)执行jk = new Number_Eng_Ban();