打开另一个窗体时关闭一个窗体

编程入门 行业动态 更新时间:2024-10-27 03:30:53
本文介绍了打开另一个窗体时关闭一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

推荐答案

为什么都这样?好的,只需使用方法System.Windows.Forms.Form.Close. 那不是全部.这个问题不那么琐碎. 再次显示相同的表单(只需使用Form.Show即可,无论是否关闭该表单都无关紧要),您需要防止表单处置.这是通过处理事件Form.FormClosing,.来完成的. Why all that? OK, just use the method System.Windows.Forms.Form.Close. That''s not all. The problem is a little bit less trivial. The show the same form again (just use Form.Show, not matter if the form was closed before or not), you need to prevent form disposal. This is done via handling the event Form.FormClosing, . //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

更多推荐

打开另一个窗体时关闭一个窗体

本文发布于:2023-11-11 06:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!