开发者

Closing a form and doing something on return to another form

I have a mainForm and a saveForm. I never close the mainForm, just let the saveForm appear over the top.

When i close the saveForm, I want a piece of code to run on returning to 开发者_如何转开发mainForm.

What is the best way to achieve this?


In addition to @benPearce's answer, if you are content to have saveForm appear modally, then you can just call:

So in the mainForm, I am assuming you have a Save button (let's call it btnSave) of some kind that brings up saveForm, right? Right. So double click on that Save button and Visual Studio will create an event handler for you. Type in the code below.

private void btnSave_Click(object sender, EventArgs e)
{
    saveForm sf = new SaveForm();
    if (sf.ShowDialog() == DialogResult.OK)
    {
        // do your thing
    }
}

Of course, you have to make sure that the saveForm is setting the DialogResult. For instance, assuming you have an OK button in the saveForm that is supposed to close the saveForm... In the Click event for the OK button you would do this:

private void btnOK_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
    this.Close();
}


In mainForm, subscribe to the FormClosed event on the saveForm, put your code in the event handler for this event

void saveForm_FormClosed(object sender, FormClosedEventArgs e)
{
    /// code here
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜