且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使多个表单在 VB.NET 中显示为一个

更新时间:2023-11-18 14:14:19

我看到这个已经在评论中了,但是我过去在这种情况下所做的是将应用程序中的每个表单"构建为自定义控件.然后我有一个实际的表单,导航通过更改当前加载到父表单上的自定义控件来工作.要从一个屏幕/视图移动到另一个屏幕/视图,请从表单的控件集合中删除当前自定义控件并添加新的自定义控件.

I see this is already in the comments, but what I have done in this case in the past is build each "form" in the application as a custom control. Then I have one actual form, and navigation works by changing which custom control is currently loaded on the parent form. To move from one screen/view to another, you remove the current custom control from the form's controls collection and add the new custom control.

我相信这比手动设置启动位置和大小要好,因为您可以使用表单的 .SuspendLayout()/.ResumeLayout() 方法向用户隐藏没有加载控件的临时状态.当您希望一种形式完全被另一种形式替代时,这很难做到.

I believe this is superior to manually setting the startup position and size, because you can use the form's .SuspendLayout()/.ResumeLayout() methods to hide the interim state, where there is no control loaded, from the user. This is harder to do when you want one form to be completely replaced by another.

这也使得在一个地方设置某些表单属性变得容易,并使它们与应用程序保持一致.您甚至可以在表单上有一个区域,其中包含现在将显示在每个视图中的控件.

This also makes it easy to set certain form properties in one place and have them be consistent for the application. You can even have an area on the form with controls that will now show in every view.

使用这种模式时,我通常让我的每个自定义控件都继承自一个公共基础.一开始你可能没有任何具体的东西可以用这个基础做,但它几乎总是在以后派上用场.

When using this pattern, I typically have each of my custom controls inherit from a common base. You may not have anything specific you will do with that base at the outset, but it almost always comes in handy later.

最后,切换到使用此方案比您想象的要容易.只需转到每个当前窗体的代码,您就会发现每个类当前都继承自 System.Windows.Forms.Form.大多数情况下,您真正​​需要做的就是将它们更改为从 System.Windows.Forms.Panel 继承,您就大功告成了.

Finally, switching to use this scheme is easier than you think. Just go to the code for the each of your current forms, and you will find that each class currently inherits from System.Windows.Forms.Form. Most of the time, all you really need to do is change them to inherit from System.Windows.Forms.Panel and you're most of the way there.