在vb.net中关闭子窗体 [英] Close child form in vb.net

查看:294
本文介绍了在vb.net中关闭子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的每个人!
现在我对父表单有疑问.
我有一个父母表格(frmMain),在父母表格中有两个子表格(frmLogin和frmClaim).
我的问题是:
当我已经登录时,我想关闭frmLogin并打开frmClaim.
我该怎么办?

Dear every body!
Now I have a problem with parent form.
I have a parents form (frmMain) and in parents form has two child forms (frmLogin and frmClaim).
My question is:
when I log in already I want to close frmLogin and open frmClaim.
How can I do?

推荐答案

首先,表单没有功能上的父子关系.确实具有Parent属性,因为它们从System.Windows.Forms.Control继承,但是尝试使任何形式的任何事物成为子对象都会引起异常,除非将System.Windows.Forms.TopLevel设置为false,这…可以尝试,但是结果看起来很难看; Form并非旨在用于这种方式.

因此,您的表格都不是子表格.一种形式是特殊的:主要形式.其中哪个主要是由调用Application.Run(formInstanceWhichIsGoingToBeTheMain)定义的.唯一的问题是:关闭主窗体,并且与其他窗体不同,整个应用程序将关闭.

至少可以有不同的方式来顺序显示表单.为简单起见,让我们仅使用两个操作:frmMainfrmLogin.所有代码都应使用您的入口点方法编写(通常为Main):

First of all, there are no functional parent-child relationships for forms. The do have Parent property because they inherit from System.Windows.Forms.Control, but an attempt to make any form a child of anything will cause and exception, unless you set System.Windows.Forms.TopLevel to false, which… well, you can try it, but the result will look ugly; the Form is not designed to be used that way.

So, none of your forms are child forms. One form is special: the main form. Which in is main is defined by the call Application.Run(formInstanceWhichIsGoingToBeTheMain). The only problem is: close a main form, and, unlike any other, the whole application will close.

There can be at least to diffent ways to show forms in sequence. For simplicity, let''s operate with only two: frmMain and frmLogin. All code should be written in your entry-point method (normally Main):

//...

Form frmMain = new FromMain();
Form frmLogin = new FormLogin();

Application.Run(frmLogin); //frmLogin becomes main
//all operations inside frmLogin should close it, no matter if login was successful or not
if (!frmLogin.LoginSuccessful) // you need to create such property, with access "internal"
   exit; //now the process exits

Application.Run(frmMain); //now frmMain becomes a main form
//when this form is closed, the process exits


二,替代方案:


Second, alternative schema:

//...

Form frmMain = new FromMain();
Form frmLogin = new FormLogin();

frmLogin.ShowDialog(); //not everyone knows that the dialog can be shown even before application is run...
if (!frmLogin.LoginSuccessful)
   exit; //now the process exits

Application.Run(frmMain);



—SA



—SA


这篇关于在vb.net中关闭子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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