本文介绍了打开另一个窗体时关闭一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.我有3种形式,形式1,形式2,形式3.
2.通过单击表单1中的不同按钮来打开表单2和表单3;
3.我想在单击按钮以打开表格3时关闭表格2(如果已打开);
反之亦然
这怎么可能?请帮助

1. I have 3 forms ,form 1,form 2,form 3.
2. I open form 2 and form 3 by clicking different buttons in form 1;
3. I want to close form 2(if opened) when i click a button to open form 3;
and vice versa
how this possible?? pls help

推荐答案

//call it from the form constructor:
this.FormClosing += (sender, evenArgs) => {
    if (evenArgs.CloseReason != CloseReason.ApplicationExitCall) {
        evenArgs.Cancel = true;
        this.Hide();
    } //if
};



或者,重写方法System.Windows.Forms.Form.OnFormClosing可获得相同的效果.需要检查CloseReason才能在关闭应用程序时采取常规措施.

重要:如果此技巧也应用于主窗体,则可能会产生以下问题:如何关闭应用程序?好了,您将需要添加一个控件或菜单​​项来完成此操作.它应该显式调用Application.Exit.

总体而言,您尝试实现的UI样式非常糟糕:不仅难以实现,而且还很难实现.这也使用户感到困惑.考虑以下情况:您只使用一个主窗口,但是它改变了它的外观,因为它是填充表单客户区的3个面板的父窗口.您一次只显示一个面板.或者,仅创建基于System.Windows.Forms.TabControl的选项卡式UI,这是最简单自然的方法.

—SA



Alternatively, override the method System.Windows.Forms.Form.OnFormClosing to get the same effect. The check of CloseReason is needed to allow regular action on closing of the application.

Important: If this trick is applied to the main form as well, it can create the following problem: how to close the application? Well, you will need to add a control or a menu item to do that; it should call Application.Exit explicitly.

Overall, the style of UI you''re trying to implement is pretty bad: not only it''s harder to implement; it is also confusing the users. Consider the following: you use just one main window, but it changes its appearance because it is a parent of 3 panels filling the form client area; you show only one panel at a time. Alternatively, create just a tabbed UI based on System.Windows.Forms.TabControl — this is the easiest and a very natural way.

—SA


这篇关于打开另一个窗体时关闭一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 15:50
查看更多