简体   繁体   English

从消息框中关闭所需的表单! (C#)

[英]Closing the desired form from a messagebox! (c#)

Here's how my forms are - Form1 is the first form. 这是我的表单的样子-Form1是第一个表单。 From Form1 I move to Form2 by using showdialog method (Form1 is in the background and Form2 on top). 我使用showdialog方法从Form1移到Form2(Form1在后台,Form2在顶部)。 Now on clicking a button on Form2, a messagebox is shown (mind you, Form1 is still in the background). 现在,单击Form2上的按钮时,将显示一个messagebox (请注意,Form1仍在后台)。 Messagebox has just the OK button. Messagebox只有“确定”按钮。 Now, when I press OK I want to load Form3 and want to close both Form2 and Form1. 现在,当我按OK我想加载Form3并想同时关闭Form2和Form1。 How can I close Form2 and Form1?? 如何关闭Form2和Form1? I used this code in Form2: 我在Form2中使用了以下代码:

    private void button1_Click(object sender, EventArgs e)
    {
         if (...)
         {
               MessageBox.Show("hello");
               this.DialogResult = DialogResult.OK;
               this.Close();
               Form3 frm = new Form3();
               frm.ShowDialog();
         }
    }

This method doesn't close Form2 and Form1 but Form3 is showed. 此方法不会关闭Form2和Form1,但会显示Form3。 So I tried this in Form2: 所以我在Form2中尝试了这个:

    private void button1_Click(object sender, EventArgs e)
    {
        if (...)
        {
           if (MessageBox.Show("hello") == DialogResult.OK)
           {
               this.Close();
               Form3 frm = new Form3();
               frm.ShowDialog();
           }
        }
    }  

Still both the forms aren't closed. 仍然没有关闭两个表格。 I tried calling a public close method ( this.Close in Form1 and Form2) created in Form1 and Form2 from Form2 under the MessageBox.Show . 我尝试从MessageBox.Show下的Form2调用在Form1和Form2中创建的公共关闭方法(在Form1和Form2中为this.Close )。 Still nothing worked. 仍然没有任何效果。 How to get rid of both forms with message box's OK button?? 如何使用消息框的“确定”按钮删除这两种形式?

Thanks. 谢谢。 Simple but tricky.. Kindly leave code snippet. 简单但棘手。请留下代码段。 I dont understand technical terms unfortunately :-( 不幸的是,我不理解技术术语:-(

You need to know the instances (forms in this case) that you want to close. 您需要知道要关闭的实例(在这种情况下为表单)。

In Form2, you could create a variable (eg theOneForm ) to store a reference to Form1 in and set it after creating Form1 (or even in the constructor of Form1). 在Form2中,您可以创建一个变量(例如theOneForm )来存储对Form1的引用,并在创建Form1(甚至在Form1的构造函数中)后对其进行设置。

// in Form2
public Form1 theOneForm {get; set;}

// in Form1
Form2 frm = new Form2();
frm.theOneForm = &this; // correct me if I'm wrong here...

Then from your button1_Click , call theOneForm.Close() - this should close your Form1. 然后从您的button1_Click调用theOneForm.Close() -这将关闭您的Form1。

private void button1_Click(object sender, EventArgs e)
{
    if (...)
    {
       if (MessageBox.Show("hello") == DialogResult.OK)
       {
           Form3 frm = new Form3();
           frm.ShowDialog();
           theOneForm.Close();
           this.Close();
       }
    }
} 

Also, close your form after you executed the other code. 另外, 执行其他代码 ,请关闭表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM