Windows 窗体:等待另一个窗体关闭 [英] Windows Forms: wait until another form closes

查看:24
本文介绍了Windows 窗体:等待另一个窗体关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,formA 需要更新一些数据,但只有在 formB 关闭之后.有没有一种简单的方法可以做到这一点?

I have a situation in which formA needs to update some data, but only after formB has closed. Is there a simple way to do this?

private void newProjectButton_Click(object sender, EventArgs e)
{
    NewProjectForm newProjectForm = new NewProjectForm();
    newProjectForm.Show();

    //wait for newProjectForm to close...

    DataTable dt = Util.ToDataTable(ProjectParticipantTable.GetUserProjectsDetails(Util.currentUserId));
    userProjectsDGV.DataSource = dt;
}

推荐答案

你必须使用 ShowDialog() 方法,并管理 DialogResult 属性.为此,您必须在 NewProjectForm 的某些部分设置 DialogResult 设置,这可以在按钮中设置属性,或者简单地通过代码设置.这会将 DialogResult 触发到您的父表单并关闭该表单

You have to make use of ShowDialog() method, and managhe the DialogResult property. For doing that, you have to set the DialogResult set on some part of NewProjectForm, that can be done setting the property in a button, or simply setting it by code. That will fire the DialogResult to your parent form and close the Form

你可以这样做:

using(NewProjectForm newProjectForm = new NewProjectForm())
{
   if(newProjectForm.ShowDialog() == DialogResult.OK)
   {
       DataTable dt = Util.ToDataTable(ProjectParticipantTable.GetUserProjectsDetails(Util.currentUserId));
       userProjectsDGV.DataSource = dt;
   }
}

这篇关于Windows 窗体:等待另一个窗体关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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