如何打开和关闭表单 [英] How to open and close a form

查看:138
本文介绍了如何打开和关闭表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

&我有两种形式; InputForm和PrintAvailFrm。

我在InputForm上有一个打印按钮,代码如下:

&I have two forms; InputForm and PrintAvailFrm.
I have a print button on the InputForm with the following code;

Private Sub PrintAvail_Click(sender As Object, e As EventArgs) Handles PrintAvail.Click
        PrintAvailFrm.Show()
        PrintAvailFrm.PAOpponents.Text = Me.Opponents.Text 'Read data on Input form and writes it on the PrintAvailFrm
        PrintAvailFrm.PAVenue.Text = Me.Venue.Text
        PrintAvailFrm.PADate.Text = Me.DatePicker.Text
        PrintAvailFrm.PATime.Text = Time.Text
        PrintAvailFrm.PATeamSize.Text = TeamSize.Text
        PrintAvailFrm.PAMatchCaptain.Text = MatchCaptain.Text

        Me.Close()
    End Sub







当我尝试运行代码时出现此错误;

MatchManagement.exe中出现未处理的System.InvalidOperationException类型异常



附加信息:创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:找不到适合指定文化或中性文化的任何资源。确保在编译时将MatchManagement.PrintAvailFrm.resources正确嵌入或链接到程序集MatchManagement中,或者确保所需的所有附属程序集都是可加载和完全签名的。




有谁能告诉我我需要做些什么来解决这个问题?



谢谢




When I try and run the code I get this error;
An unhandled exception of type 'System.InvalidOperationException' occurred in MatchManagement.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MatchManagement.PrintAvailFrm.resources" was correctly embedded or linked into assembly "MatchManagement" at compile time, or that all the satellite assemblies required are loadable and fully signed.


Can anyone tell me what I need to do to solve this?

Thanks

推荐答案

好了,详见内部异常,并提供全面的表达式信息,特别是堆栈,并显示引发异常的代码;只有这样你才能希望得到一些帮助。



现在,关于打开和关闭表格。矛盾的是,甚至认为表格可以被关闭,没有打开表格这样的东西。来想一想,从未关闭的任何东西都可以打开。你创建一个表单,显示它;就这样。至于其余的代码显示,你做错了。实际上,在您显示表单然后关闭时的代码几乎在所有情况下都是无用的。你很少需要关闭表格;在实践中,您只需要在最后关闭主表单,这将导致应用程序退出。让我们考虑不同的模型。



首先,您可以让用户使用表单,直到完成所有更改,而不是触及其他表单。然后你需要使它 modal ,这是使用 System.Windows.Forms.Form.ShowDialog

完成的 https://msdn.microsoft.com/en-us/library/ c7ykbedk%28v = vs.110%29.aspx [ ^ ]。



如果你不希望它是模态的,那就更难了,因为使用有更多的选择。关闭这样的表单毫无意义,但用户可以关闭它。即便如此,最好还是处理它并将其隐藏起来。只有这样,该形式才能在以后再次使用,处于同一状态。这是通过处理事件 Form.FormClosing 来完成的:

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing% 28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs。 110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows .forms.formclosingeventargs%28v = vs.110%29.aspx [ ^ ]。



此活动可以取消以防止实际结束。请改为 Form.Hide 。仅适用于 CloseReason 等于 System.Windows.Forms.CloseReason.UserCloseing ,以允许在结束时实际关闭应用程序生命周期。



此外,在这种情况下,您最终会同时打开多个非模态表单,请确保使用所有者拥有的关系。这足以使主表单拥有所有这些表单:

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner(v = vs.110)的.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform(v = vs.110)的.aspx [<一个href =https://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ,

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.removeownedform(v = vs.110).aspx [ ^ ]。



很难解释行为的变化,更容易使表格拥有并观察行为的差异。拥有保持应用程序的完整性;您将所有形式的相同应用程序保存在Z顺序中。如果你不这样做,任何其他应用程序的表单都可以在你的应用程序表单之间进行,这很难看,不方便。

通常,对于拥有的表单,它还有一个选项:不在任务中显示它们bar: https://msdn.microsoft .com / zh-cn / library / system.windows.forms.form.showintaskbar(v = vs.110).aspx [ ^ ]。



最后,更好的想法是避免使用那些非模态形式,并将所有功能保存在一个单一的形式中(我不计算模态形式)。您可以使用面板,标签页( TabControl )或其他内容来代替单独的表单。您可以构建更复杂的对接界面,如Visual Studio,但这是一项非常复杂的工作(或者您可以使用第三方解决方案)。简单的选项卡界面非常有效。



-SA
Well, see the inner exception for detail, and provide comprehensive expression information, especially the stack, and show the code where the exception was thrown; only then you can hope some help on this part.

Now, as to the opening and closing a form. Paradoxically, even thought a form can be closed, there is no such thing as "open a form". Come to think about, how anything which was never closed can be "opened". You create a form, show it; that's all. As to the rest of the code showing, you do it all wrong. Practically, the code when you shown a form and then close is useless in nearly all cases. You rarely need to close a form; in practice, you only need to close the main form at the very end, which leads, to application exit. Let's consider different models.

First, you can let the user work with the form until all changes are done, not touching other forms. Then you need to make it modal, which is done using System.Windows.Forms.Form.ShowDialog:
https://msdn.microsoft.com/en-us/library/c7ykbedk%28v=vs.110%29.aspx[^].

If you don't want it to be modal, it's more difficult, because the use has more choices. Closing such form makes no sense at all, but the user can close it. Even it that case, it's better to handle closing it and hide it instead. Only then that form can be used later again, in the same state. This is done by handling the event Form.FormClosing:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs%28v=vs.110%29.aspx[^].

This event can be cancelled to prevent actual closing. Call Form.Hide instead. Do it only for CloseReason equal to System.Windows.Forms.CloseReason.UserCloseing, to allow actual closing at the end of application lifetime.

Also, as in this case you end up having several non-modal forms opened at the same time, make sure you use the owner-owned relationships. It's enough to make all such forms owned by the main form:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.removeownedform(v=vs.110).aspx[^].

It's harder to explain the change in behavior, it's easier to make the forms owned and watch the difference in behavior. Owning keeps the application integrity; you keep all forms of the same application together in Z-order. If you don't, any other application's form can get between your application's forms, which is ugly, inconvenient.
Usually, for owned forms, it comes with one more option: not showing them in a task bar: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showintaskbar(v=vs.110).aspx[^].

And, finally, even better idea would be avoiding those non-modal forms at all and keep all functionality in one single form (I don't count modal forms here). Instead of separate forms, you can use panels, tab page (of the TabControl) or something else. You can build more complex docking interface like the one of Visual Studio, but this is a very complicated work (or you can use 3rd-party solutions). Simple tabbed interface works very well.

—SA


不知何故PrintAvail.vb为空并且表格已被删除。重新创建表格,一切正常。



感谢您的帮助。
Somehow the PrintAvail.vb was empty and the form had been deleted. Recreated the form and all is OK.

Thanks for any assistance.


这篇关于如何打开和关闭表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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