使多个表单在 VB.NET 中显示为一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18191450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making multiple forms appear as one in VB.NET
提问by andrew.cuthbert
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.
我正在 VB.NET 中编写 Windows 窗体应用程序。我有三种表单:主表单,显示账户列表,账户表单,允许用户查看/编辑特定账户的信息,以及策略表单,允许用户查看/编辑信息该帐户的特定政策。我希望表单看起来好像它们都是同一个窗口。示例:应用程序启动时,用户单击主窗体列表框中的帐户名称,然后单击“编辑”。我想要发生的是窗口保持在完全相同的位置并保持相同的大小,只有主表单的内容似乎被帐户表单的内容替换。如果用户然后选择从帐户表单编辑策略,则同样的事情。当用户完成并单击“保存”时,主要形式回来了。在整个用例中,用户看起来就好像他们一直在查看同一个窗口,而该窗口的内容却在不断变化。
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.
问题是如果用户移动原始窗口,新窗口会出现在父窗体最初出现的位置,而不是它结束的位置。
回答by Joel Coehoorn
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.
我看到这已经在评论中了,但是我过去在这种情况下所做的是将应用程序中的每个“表单”构建为自定义控件。然后我有一个实际的表单,导航通过更改当前加载到父表单上的自定义控件来工作。要从一个屏幕/视图移动到另一个屏幕/视图,请从表单的控件集合中删除当前自定义控件并添加新的自定义控件。
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.
我相信这比手动设置启动位置和大小要好,因为您可以使用表单的 .SuspendLayout()/.ResumeLayout() 方法向用户隐藏没有加载控件的临时状态。当您希望一种形式完全被另一种形式取代时,这很难做到。
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.
使用这种模式时,我通常让我的每个自定义控件都继承自一个公共基础。一开始你可能没有任何具体的东西可以用这个基础做,但它几乎总是在以后派上用场。
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.
最后,切换到使用此方案比您想象的要容易。只需转到每个当前表单的代码,您就会发现每个类当前都继承自 System.Windows.Forms.Form。大多数情况下,您真正需要做的就是将它们更改为从 System.Windows.Forms.Panel 继承,您就大功告成了。
回答by Grim
As others have said, it may be better to redesign your application using custom controls or panels etc.
正如其他人所说,使用自定义控件或面板等重新设计应用程序可能会更好。
However, to answer your question regarding the seemingly random location of your forms, the first thing to check is that each form has it's StartPositionproperty set to Manual.
但是,要回答有关表单看似随机位置的问题,首先要检查的是每个表单的StartPosition属性都设置为 Manual。
If your main form is resizable, then I would also add code to adjust newFormto the same size too.
如果您的主窗体可调整大小,那么我也会添加代码以调整newForm到相同的大小。
I hope that helps with your immediate issues; so that you can move on to redesigning the application!
我希望这有助于您解决眼前的问题;以便您可以继续重新设计应用程序!
回答by moayadmyro
good morning there is another way . set property for second form to (top most) and use also
早上好 还有另一种方式。将第二个表单的属性设置为(最上面)并使用
from2.show();
that make you switch between forms and keep form2 top other
这使您可以在表单之间切换并使 form2 保持在其他状态
Thanks
谢谢
回答by Jade
try using ShowDialog()
尝试使用 ShowDialog()
Dim newForm as New AcctForm
newForm.Location = Me.Location
newForm.ShowDialog()
Me.Close() <-- removed this

