从C#中的第一个表单关闭第二个表单 [英] Close second form from first form in C#

查看:86
本文介绍了从C#中的第一个表单关闭第二个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好先生/妈妈



我在c#中创建了2个表单。



我想从c#中的第一种形式关闭第二种形式(不存在)。



下面的代码无效。



2nd_form 2n = new 2nd_form();

2n.close();



请帮忙。



我是什么尝试过:



Hello sir/Mam

I have created 2 forms in c#.

I want to close 2nd form (not existing) from 1st form in c#.

Below code is not working.

2nd_form 2n = new 2nd_form();
2n.close();

Please help.

What I have tried:

Below code is not working.

2nd_form 2n = new 2nd_form();
2n.close();

Please help.

推荐答案

您必须首先显示表格才能关闭。

你可以试试这个:

You have to show a form first before it can be closed.
You can try this:
2nd_form 2n = new 2nd_form();
2n.Show();
// Your code here e.g. Thread.Sleep(5000)
// or put a breakpoint on the next line
2n.Close();


它不能work:你正在关闭你刚刚创建的表单的实例 - 这就是 new 意味着



想一想:如果你将手机放在汽车的手套箱中然后买一辆新车,你会打开新的手套箱并找到你的手机那里?当然不是 - 手套箱是旧车的一部分,与新车完全分开。



关闭用户正在寻找的版本您需要找到它 - 这意味着查看您显示表单的代码并找出存储实例的位置。
It cant work: you are closing an instance of the form that you just created - that's what new means!

Think of it this way: If you put your phone in the glove box of your car and then buy a new car would you expect to open the new glove box and find your phone there? Of course not - the glove box is part of the old instance of a Car and that is entirely separate from the new Car.

To close the version the user is looking at you need to locate it - which means looking at your code where you Show the form and working out where you have stored the instance.


这篇关于从C#中的第一个表单关闭第二个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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