“切换"的最佳方式在面板上的两个用户控件之间? [英] Best way to "toggle" between two UserControls on a panel?

查看:34
本文介绍了“切换"的最佳方式在面板上的两个用户控件之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个单独的 UserControls 并且(取决于选择了哪个 RadioButton)我想要显示其中一个.

现在,我只是将每个 UserControl 的一个实例拖到表单上,然后将一个实例放在另一个上面(设置一个 .Visible = false;)..>

这没问题,但我想知道是否有更好或更合适的方法来做到这一点?

解决方案

这完全合理.

如果您担心保留不再使用的资源,您可以添加一个 Panel 并从那里添加或删除控件.

例如

试试{panel.SuspendLayout();panel.Controls.Remove(userControl1);panel.Controls.Add(userControl2);}最后{panel.ResumeLayout();}

如果用户控件执行诸如连接到数据源之类的操作,您可能希望实际处置并重新创建它们.实际上取决于控件的复杂程度.如果他们只是捕获一些属性,那么您当前的解决方案就可以了.

I have created two separate UserControls and (depending on which RadioButton is selected) I would like one or the other to be displayed.

Right now, I simply dragged one instance of each UserControl onto the form and placed one on top of another (setting one .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.

E.g.

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.

这篇关于“切换"的最佳方式在面板上的两个用户控件之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆