如何使用form1关闭form2? [英] How to close form2 using form1?

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

问题描述

How to Close form2 using form1?





我尝试过:



private void button_Click(object sender,EventArgs e)

if(radio1.Checked)

{

关闭frm2 = new关闭();

f2.Close();

}



What I have tried:

private void button_Click (object sender, EventArgs e)
if (radio1.Checked)
{
Close frm2 = new Close();
f2.Close();
}

推荐答案

假设这是:



1. WinForms



2. Form1是主要表格创建并显示

'f2表格



3. Button和RadioButton在Form1上

Assuming this is:

1. WinForms

2. Form1 is the main Form which created, and made visible, the
'f2 Form

3. the Button and RadioButton are on Form1
private void button_Click (object sender, EventArgs e)
{    
    if (radio1.Checked)
    {
        if(f2 != null) f2.Close();
    }
}

你在这里使用CheckBox吗?为什么你甚至需要一个按钮?你检查处理它的想法似乎很奇怪。



考虑你是否应该隐藏而不是关闭'f2。



...编辑...复选框示例

Should you be using a CheckBox here ? Why do you even need a Button ? The idea you check something to dispose it seems odd.

Consider if you should hide, rather than close, 'f2.

... edit ... CheckBox example

Form2 f2 = new Form2();

const string CbxShowTxt = "Show Second Form";
const string CbxHideTxt = "Hide Second Form";

private void Form1_Load(object sender, EventArgs e)
{
    checkBox1.Text = CbxShowTxt;
    checkBox1.Checked = false;
}

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        f2.Show();
        checkBox1.Text = CbxHideTxt;
    }
    else
    {
        f2.Hide();
        checkBox1.Text = CbxShowTxt;
    }
}

...结束编辑...

... end edit ...


试试这样



Try like this

Form2 obj = (Form2)Application.OpenForms["Form2"];
obj.Close();


试试这个...



try this...

if (radio1.Checked == true)
{
Form1 frm = new Form1();
frm.Show();
this.Hide();
}


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

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