如何通过按钮单击关闭一个窗体通过另一个窗体 [英] how to close one form through another form by button click

查看:73
本文介绍了如何通过按钮单击关闭一个窗体通过另一个窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过按钮点击另一个表单关闭一个表单。



我尝试使用下面的代码,但它不起作用:



how to close one form through another form by button click.

I tried using the below code , but it doesn't work:

private void btnInventory_Click(object sender, EventArgs e)
{           
            frmCustomer f = new frmCustomer();
            f.Close();
           
}





什么是正确的方式?



感谢您的反馈。

感谢您



What is the right way ?

Appreciate your feedback.
Thanking You

推荐答案

您好,



Hi,

frmCustomer f = new frmCustomer();
f.Close();





无法正常工作,因为此f是在本地创建的。你需要找到现有的frmCustomer实例然后关闭它而不是创建一个要关闭的新实例。



你的代码看起来像什么:





can not work since this "f" is created locally. You need to find the existing instance of "frmCustomer" and then close this instead of creating a new instance to close.

What your code could look like:

frmCustomer f;

private void btnShowDialog_Click(object sender, EventArgs e)
{
  if(f == null)
    f = new frmCustomer();
  
  f.Show();
}

private void btnInventory_Click(object sender, EventArgs e)
{
  if(f != null)
    f.Close();
}


您尝试过的代码永远不会有效。您创建新对象 frmCustomer 并在打开它之前关闭它。您必须明白,当您应用 new 时,您将创建一个完全新对象。此对象与先前创建的对象的相同但数据不一样。因此,您无法通过简单地使用该的其他对象 =cs>对象。



要关闭此表格,您需要实际引用表单。因此,您需要将此内容提供给 frmInventory (例如,您可以添加一个可以设置此引用的属性)并使用它来关闭另一个表格



另一种方法是在所有打开的表单表单 c $ c>你的应用程序是这样的:

The code you tried will never work. You create a new object of frmCustomer and close it before ever opening it. You must understand that when you apply new you create a completely new object. This object is of the same class of an earlier created object but the data is not the same. Therefore you cannot control the other objects of the same class by simply using another instance of that object.

To close this Form you would need the actual reference to that Form. So you would need to give this to your frmInventory (you could for example add a property where you can set this reference) and use that to close the other Form.

Another way is to look for that Form in all open Forms of your Application like this:
for (int index = Application.OpenForms.Count-1; index>= 0; index--)
{
    if (Application.OpenForms[1].Name == "Customer")
    {
        Application.OpenForms[1].Close;
    }
}



请注意,使用 foreach 这样可能会导致在例外


Be aware that using a foreach like this can result in an Exception:

foreach (Form form in Application.OpenForms)
{
    // ...
}



这是因为你改变了循环中的列表,这可能会给你一个无效的操作异常。



祝你好运!


This is because you altered the list within the loop which might give you an invalid operation exception.

Good luck!


线索在于:

The clue is in the word:
frmCustomer f = new frmCustomer();
f.Close();

new 关键字有什么作用?



您是否希望我的车钥匙适合您的汽车点火?如果它确实存在并且我开走了你会不会烦恼?



那你为什么期望关闭一个新的表格实例来关闭一个不同的现有实例呢? br />


找到现有表单的实例,然后关闭它。你在哪里打开它?看那里,希望这意味着你将在某处保存实例。如果没有,你将不得不这样做。或者使用Application对象获得创意 - 真的更好地保存实例。

What does the new keyword do?

Would you expect my car key to fit your car ignition? Would you be annoyed if it did and I drove away?

So why do you expect that closing a new instance of a form to close a different, existing instance?

Find the instance of the existing form, and close that. Where did you open it? Look there, and hopefully that means you will have saved the instance somewhere. If not, you will have to. Or get creative with the Application object - better to save the instance, really.


这篇关于如何通过按钮单击关闭一个窗体通过另一个窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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