父/子窗口窗体的隐藏/显示,C# [英] Hide/Show of parent and child window form, C#

查看:931
本文介绍了父/子窗口窗体的隐藏/显示,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当显示子级并且再次关闭子级窗口时,我希望隐藏父级窗口.父级窗口应该再次可见.

I wan to Hide Parent window when it''s child will be shown and when child window will be closed again parent window should again visible.

//parent window calling child form//
ChildForm s=new ChildForm();
s.show();
this.hide();


//buuttton on child forn to close it//

Form p=(Form)this.Parent;
p.show();
this.close();





It''s not working how to do that.

推荐答案

首先,您应该了解子窗体依赖关系已失效.您可以通过将表单的TopLevel分配为false来覆盖它,但是……甚至不要尝试,这样做没有任何好处;您只会将一种形式放入另一种形式,这没有任何意义.

您的代码不起作用",通常是因为表单的this.Parent实际上是空的.同样,如果您打算再次显示,则永远不要关闭表单.您最好将其隐藏(也在FormClosing事件中).

您需要更改设计以控制可见性,这根本不是问题.首先,我建议仅使用一种形式.现在是什么表单,可以是在表单中显示或隐藏的单独面板.如果您仍然需要其他表格(我不建议这样做),则需要使用隐藏和显示而不是关闭.问题在于,显示一种形式并隐藏另一种形式的代码应该在已知两种形式的引用的内容中进行.形式几乎是独立的.一种形式不知道其他形式,因此您需要提供对另一种形式的引用.如何做到这一点,取决于您的目的,但微不足道.

—SA
First of all, you should understand that child-form dependency is rendered defunct for forms. You can override it by assigning TopLevel of the form to false, but… don''t even try, there is nothing good on doing so; you would simply get one form inside another, which makes no sense.

You code is "not working", just because this.Parent for a form is really null, normally. Also, you should never close a form if you plan on showing again; you better need to hide it (in the FormClosing event, too).

You need to change the design to control the visibility, which is not a problem at all. First of all, I would recommend to use only one form. What are now forms, could be separate panel showing or hiding in the form. If you still need more forms, which I would not recommend, you need to use hiding and showing, not closing. The problem is that the code showing one form and hiding another form should be done in the content where the references to both forms are already known. The forms are pretty much independent; one form is unaware of others, so you need to provide a reference to another form. How to do it, depends on your purpose, but is trivial.

—SA


这篇关于父/子窗口窗体的隐藏/显示,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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