使多个表单在 VB.NET 中显示为一个 [英] Making multiple forms appear as one in VB.NET

查看:22
本文介绍了使多个表单在 VB.NET 中显示为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 VB.NET 中编写 Windows 窗体应用程序.我有三种表单:主表单,显示账户列表,账户表单,允许用户查看/编辑特定账户的信息,以及策略表单,允许用户查看/编辑信息该帐户的特定政策.我希望表单看起来好像它们都是同一个窗口.示例:当应用程序启动时,用户在主窗体上的列表框中单击一个帐户名称,然后单击编辑".我想要发生的是窗口保持在完全相同的位置并保持相同的大小,只有主表单的内容似乎被帐户表单的内容替换.如果用户随后选择从帐户表单编辑策略,则情况相同.当用户完成并单击保存"时,主窗体会重新出现.在整个用例中,用户看起来好像一直在查看同一个窗口,而该窗口的内容却在不断变化.

I am writing a Windows Forms application in VB.NET. I have three forms: the main form, which shows a list of accounts, the account form which allows the user to view/edit the information for a specific account, and the policy form which allows the user to view/edit the information on a specific policy for that account. I want the forms to appear as if they are all the same window. Example: when the application starts, the user clicks an account name in the list box on the main form and clicks "edit". What I want to happen is that the window stays in the exact same place and stays the same exact size, only the content of the main form appears to be replaced with the content of the account form. Same thing if the user then chooses to edit a policy from the account form. When the user finishes and clicks "save", the main form comes back up. Through this entire use case, it would appear to the user as if they were viewing the same window the entire time, with the content of that window changing.

我该怎么做?我试过类似的东西:

How can I do this? I have tried something like:

Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.Show()
Me.Close()

问题在于,如果用户移动原始窗口,新窗口会出现在父窗体最初出现的位置,而不是它结束的位置.

The problem is that if the user moves the original window, the new window appears where the parent form originally appeared, not where it ended up.

推荐答案

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

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.

这篇关于使多个表单在 VB.NET 中显示为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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