使表格自身关闭 [英] make form close itself

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

问题描述

需要帮助..

如何在Visual Basic 2010中使表格自行关闭并打开另一个表格?

例如...我有一个登录程序,当用户单击提交"按钮时,将出现另一个表单,并且登录表单本身将关闭....



请帮助...

Thanks

need help..

how do you make a form close itself in visual basic 2010 and open another form?

example... i have a login program and when the user clicks the submit button, another form will appear and the login form itself will close....



pls help...

thanks

推荐答案

这是在新手中最流行的最差的设计之一.问题是:您必须在不关闭应用程序的情况下关闭主窗体.查看您的入口点(通常为Program.Main).用Application.Run看那一行;此方法的参数定义哪种形式是主要形式.您必须先退出应用程序,才能更改主窗体.实际上,您可以在一个程序中执行此操作:使用相同的Main方法启动另一个Application.以灵活的方式实现此逻辑非常困难.

由于这些原因,通常的解决方案是隐藏表单而不是使用Form.Hide关闭.您还可以通过将true的值分配给事件参数的属性Cancel来覆盖方法OnFormClosing,以防止关闭表单.

这将使应用程序无法退出,因此不要忘记从某些菜单项或其他地方显式调用Application.Exit.

我建议更改整个方法.尝试仅使用一种形式,但显示UI的不同部分并隐藏其他部分.不错的样式之一是基于TabControl的选项卡UI.您第二(第三等)表格的内容将转到一个单独的选项卡,仅此而已.实际上,正确实施也要容易得多.

—SA
This is one of the worst designs somehow popular in newbies. The problem is: you cannot close main form without closing of the application. Look at you entry point (usually Program.Main). Look at the line with Application.Run; the argument of this method define which form is the main one. You cannot change the main form without exit of the application. You actually can do it in one program: start another Application in the same Main method. It is very difficult to implement this logic in a flexible way.

By these reasons, the usual solution is hiding form instead of closing using Form.Hide. You can also prevent closing a form by overriding the method OnFormClosing by assigning a value of true to the property Cancel of the event arguments.

It would make the application impossible to exit, So don''t forget to call Application.Exit explicitly from some menu item or elsewhere.

I recommend to change the whole approach. Try to work with just one forms but showing different parts of UI and hiding others. One of the nice styles is the tab UI based on TabControl. What was you second (third, etc.) form will go to a separate tab, that''s it. It''s actually also much easier to implement correctly.

—SA


在提交"按钮单击事件中执行Form.Close()以关闭表单.

要打开另一个表格,您需要决定应该在哪里完成.我建议一种可能性是使登录表单成为模态对话框,然后在验证之后,以确保登录名有效后以与打开登录表单相同的过程打开新表单,等等.我可能会检查是否有效在模式对话框返回时,在登录表单的提交"按钮事件处理程序中登录,然后将结果作为OKYesNo等作为DialogResult传递给调用过程. >
您如何选择它取决于您的设计.
Do a Form.Close() in the submit button click event to close the form.

To open another form, you need to decide where that should be done. I would suggest that one possibility is to make the login form a modal dialog, then after validating, open the new form in the same procedure that opened the login form after ensuring that the login was valid, etc. I would probably check for a valid login in the login form''s submit button event handler, then pass the result as OK, Yes, No, etc. as a DialogResult to the calling procedure when the modal dialog returns.

How you choose to do it depends on your design.


这篇关于使表格自身关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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