简体   繁体   中英

Form Closing Event visual studio c#

Straight to the problem: In my main form, i have a three buttons that open three different forms. I will show you how it is built.

  • MainForm (Here is three buttons, with the three different form names on them)

    • Theory -> Click this button to open TheoryForm
    • Tasks -> Click this button to open TasksForm
    • Compete -> Click this button to open CompeteForm

Inside my TasksForm is a button that is going to open the TheoryForm . Here is my code:

public partial class TasksForm : Form
{
     public TasksForm()
     {
          InitializeComponent();
     }

     public void TheoryButton_Click(object sender, EventArgs e)
     {
          Form TheoryForm_Child = new TeoriForm();
          TheoryForm_Child.Show();
     }
     //Add some code here so that when `TasksForm` closes, the `TheoryForm_Child` closes too.
}

And what I can't figure out is, when the TasksForm is closed, the TheoryForm is supposed to close as well, right now it doesn't.

Try declaring the variable to your TheoryForm outside of the TheoryButton.Click event handler and then use it in your TaskForm.FormClosing event handler to close it.

public partial class TasksForm : Form
{
    private Form TheoryForm_Child;

    public TasksForm()
    {
        InitializeComponent();
        FormClosing += TaskForm_FormClosing;
    }

    public void TheoryButton_Click(object sender, EventArgs e)
    {
        TheoryForm_Child = new TeoriForm();
        TheoryForm_Child.Show();
    }

    public void TaskForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(TheoryForm_Child != null)
            TheoryForm_Child.Close();
    }
}

Just because TasksForm is creating TheoryForm does not mean that when TasksForm is closed, so will TheoryForm. Instead, you should close it explicitly like by handing the closed event in your TasksForm, like this.

public partial class TasksForm : Form
{
    Form _TheoryFor_Child = new TheoryForm();

    public TasksForm()
    {
      InitializeComponent();
      Closed += TasksForm_Closed;
    }

    private void TasksForm_Closed(object sender, EventArgs e)
    {
      _TheoryFor_Child.Close();
    }

    private void TheoryButton_Click(object sender, EventArgs e)
    {      
      _TheoryFor_Child.Show();
    }
}

You need to connect parent and child form in some way. For example in giving the child form the parent form as owner.

Simply call

 TheoryForm_Child.Show(this);

There is a really simple solution. You should use the other version of Show method like this:

Form TheoryForm_Child = new TeoriForm();
TheoryForm_Child.Show(this);

That's all. Then your form will be owner of theory form. So it will automagically destroy Theory form after closing itself.

More reading here: https://msdn.microsoft.com/en-us/library/szcefbbd%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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