C#-关闭表单后启动功能 [英] C# - Start Function After Closing Form

查看:52
本文介绍了C#-关闭表单后启动功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2种表格.一个是显示表单,另一个是将数据添加到数组的位置.

I have 2 forms. One is the display form and the other is the where data is added to an array.

在添加到数组表单上单击确定按钮后,如何在显示表单上启动功能?

How do I start a function on the display form once the ok button has been clicked on the add to array form?

让我改一下.我将如何从另一个.cs文件中调用一个.cs文件中的函数.

Let me rephrase. How would I go about calling a function that is in one .cs file, from another .cs file.

表格1

private void menuItem1_Click(object sender, EventArgs e)
{
    Form2 form2= new Form2();
    form2.Owner = this;
    form2.ShowDialog(this);
}

表格2

private void button1_Click(object sender, EventArgs e)
{
    Form1 form1 = new Form1();
    form1.myMethod();
}

很显然,form2代码正在生成一个新的form1实例.

Obviously, the form2 code is generating a new instance of form1.

如何运行打开模式窗口的窗体的方法?

How do I run the method of the form that opened the modal window?

谢谢

推荐答案

Form2 的所有者是 Form1 的实例,因此您可以将所有者转换为Form1 并调用方法:

The owner of Form2 is a instance of Form1 so you can cast the owner to Form1 and call the method:

(this.Owner as Form1).myMethod();

这篇关于C#-关闭表单后启动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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