简体   繁体   English

在面板上的两个UserControl之间“切换”的最佳方法?

[英]Best way to “toggle” between two UserControls on a panel?

I have created two separate UserControls and (depending on which RadioButton is selected) I would like one or the other to be displayed. 我已经创建了两个单独的UserControls并且(取决于选择哪个RadioButton )我希望一个或另一个显示。

Right now, I simply dragged one instance of each UserControl onto the form and placed one on top of another (setting one .Visible = false; ). 现在,我只是将每个UserControl一个实例拖到UserControl上,然后将一个实例放置在另一个实例上(设置一个.Visible = false; )。

This is OK, but I was wondering if there was a better or more appropriate way to do this? 可以,但是我想知道是否有更好或更合适的方法?

That's perfectly reasonable. 那是完全合理的。

If you're concerned about keeping around resources you're no longer using, you can add a Panel and add or remove the control from there. 如果您担心要保留不再使用的资源,则可以添加一个Panel然后在其中添加或删除控件。

Eg 例如

try
{
    panel.SuspendLayout();
    panel.Controls.Remove(userControl1);
    panel.Controls.Add(userControl2);
}
finally
{
    panel.ResumeLayout();
}

If the user controls do things like connect to data sources, you might want to actually dispose and recreate them. 如果用户控件执行诸如连接到数据源之类的操作,则可能需要实际处理并重新创建它们。 Really depends on how complex the controls are. 确实取决于控件的复杂程度。 If they're just capturing a few properties, your current solution is fine. 如果他们只是捕获一些属性,那么您当前的解决方案就可以了。

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

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