开发者

Closing One Form From Another Form?

开发者 https://www.devze.com 2023-01-04 15:22 出处:网络
I have a little application that creates alerts whenever a change to a database is made. I have a few options in the alert form that pops up.

I have a little application that creates alerts whenever a change to a database is made. I have a few options in the alert form that pops up.

One of the options opens another form (a child form) asking the user for further information.

When the child form gets the necessary information from the user, I want it to close as well as the parent form. So far, I only know how to close the child form, but not the parent form.

Parent form > Opens child form
Child gathers information > User clicks ok in child > child closes, parent close开发者_如何学Gos

^this is what I want

I just don't have the brain power to think about how to communicate across forms to accomplish closing the parent form.

Any help would be much appreciated. Actually, it would be super appreciated. If I could learn how to make my forms communicate with each other, I could really do a lot of damage (in a good way 8D ).


In the parent form, you can do something like this:

ChildForm f = new ChildForm();
f.FormClosed += (o,e) => this.Close();
f.Show();


Try this in the parent form:

using (var childForm = new ChildForm())
{
    if (childForm.ShowDialog() == DialogResult.OK)
    {
        Close();
    }
}

Your child form should return a DialogResult by clicking buttons (OK or Cancel) and/or setting the AcceptButton and CancelButton properties in the designer.

0

精彩评论

暂无评论...
验证码 换一张
取 消