第二个表单关闭时,在第一个表单上调用事件

时间:2016-10-17 12:53:50

标签: c# forms

我有两种形式,我打开并showdialog第二种形式:

Recieving_Stock_Form f = new Recieving_Stock_Form();
f.Username = Username;
f.Role = Role;
f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd");
f.ShowDialog(); 

现在,当我关闭第二个表单时,我想在第一个表单上触发一个事件 E.G

void txtStockCount_TextChanged(object sender, EventArgs e)

我可以查看有关此内容或如何查看的任何想法?

由于

4 个答案:

答案 0 :(得分:3)

Form1中,假设您的代码是

Recieving_Stock_Form f = new Recieving_Stock_Form();

您可以添加代码

f.Form_Closing += ExampleHandler;

ExampleHandler(object sender, FormClosingEventArgs e)
{
    //Do stuff
}

答案 1 :(得分:2)

我宁愿不与表格作弊;相反,我建议将Recieving_Stock_Form的值分配给txtStockCount

using (Recieving_Stock_Form f = new Recieving_Stock_Form()) {
  f.Username = Username;
  f.Role = Role;
  f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd");

  // == DialogResult.OK - let user have a chance to cancel his/her input
  if (f.ShowDialog() == DialogResult.OK) {
    //TODO: put the right property here 
    txtStockCount.Text = f.StockCount.ToString();
  }
}

答案 2 :(得分:1)

ShowDialog是模态的,所以它会阻止执行,所以当你关闭它时,下一步就会被执行。

你可以这样做:

Recieving_Stock_Form f = new Recieving_Stock_Form();
f.Username = Username;
f.Role = Role;
f.Date = monthCalendar1.SelectionStart.ToString(@"yyyy\/MM\/dd");
f.ShowDialog();
// it will continue here when the form is closed.
txtStockCount_TextChanged(this, EventArgs.Empty); // or assign the variable directly.

答案 3 :(得分:1)

根据您的决定,您可以处理两个事件 - FormClosedFormClosing

f.FormClosed += f_FormClosed;

private void f_FormClosed(object sender, FormClosedEventArgs e)
{
    //Call your function or do whatever you want
}