简体   繁体   English

在父表单面板中显示用户控件

[英]Displaying user control in parent form panel

The first line is able to clear the panel but doesnt seem to show the usercontrol in the panel in the parent form.第一行能够清除面板,但似乎没有在父窗体的面板中显示用户控件。

panel1.Parent.Controls.Clear();

if (panel1.Parent == null)
    return;

messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Parent.Controls.Add(uc);

Why you use panel1.Parent ?为什么你使用panel1.Parent In this way, you remove all controls not from the panel itself, but from the parent control on which this panel is located.通过这种方式,您不是从面板本身中删除所有控件,而是从该面板所在的父控件中删除所有控件。

This action removes the panel itself.此操作会删除面板本身。 And, accordingly, the panel does not have a parent now.因此,该面板现在没有父级。 The Parent property becomes null . Parent属性变为null

I suppose you need to write like this:我想你需要这样写:

panel1.Controls.Clear();

messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Controls.Add(uc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM