如何关闭form2? [英] how to close form2 ?

查看:111
本文介绍了如何关闭form2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将显示form2,但是我想要的是,当我单击列表框"close form 2"时
表格2将不会关闭!

this code wil show form2 but what i want is , when i click in listbox "close form 2"
the form 2 will not close !

private void button1_Click(object sender, EventArgs e)
{
Form2 myForm;
myForm = new Form2();
myForm.Show();
}





private void listBox5_Click_1(object sender, EventArgs e)
{
                   if (listBox5.Text == ("close form 2"))
                     {
                         


                         Form2 myForm;
                         myForm = new Form2();
                         myForm.close();// but its not close it D:
}
}

推荐答案

您应该读一本有关OO开发的书.这段代码可以满足您的要求. button1创建一个表单并显示它. listbox5创建一个新窗体,然后关闭它(只有它从未打开过).我也认为.close是不会编译的.但是,无论哪种方式,您都需要保留对您创建并关闭的Form2实例的引用,而不是与要关闭的表单无关的随机新实例.
You should read a book on OO development. This code does what you ask. button1 creates a form and shows it. listbox5 creates a new form, and closes that ( only it never opened ). I also think that .close will not compile, it''s .Close. But, either way, you need to keep a reference to the Form2 instance you create and close THAT, not a random new instance that has nothing to do with the form you want to close.


您不能像这样关闭它-您必须关闭实际显示的Form2实例.
试试这个:
You can''t close it like that - you have to close the instance of Form2 that is actually showing.
Try this:
private Form2 myForm;
private void button1_Click(object sender, EventArgs e)
   {
   myForm = new Form2();
   myForm.Show();  
   }

private void listBox5_Click_1(object sender, EventArgs e)
   {
   if (listBox5.Text == ("close form 2"))
      {
      myForm.Close();
      }
   }


您要在listBox5_Click方法中声明一个新表单,因此它与最初创建的表单不同.

您需要将Form2创建为一个变量(例如myForm),可以在您的所有基类中进行访问

所以在button1_click下,您只需要显示表单
myform.Show();

并在listBox5_Click下会是
myform.Close();

显然,您将不得不首先在其他地方实例化表单(也许是讲师)
You''re declaring a new form in the listBox5_Click method, so it''s not the same form as the one initially created.

You need to create Form2 as a variable (e.g. myForm) that can be accessed throughout your base class

so under button1_click you only need to show the form
myform.Show();

and under listBox5_Click it would be
myform.Close();

You would obviously have to instantiate your form first somewhere else (instructor perhaps)


这篇关于如何关闭form2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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