在 C# 中从另一个子窗体调用一个子窗体 [英] calling one child form from another child form in c#

查看:34
本文介绍了在 C# 中从另一个子窗体调用一个子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个表单:FormA、FormB 和 FormC,其中 FormA 是 mdiParent 表单,表单 B 和 C 是子表单.我在 FormA 加载事件中编写了以下代码.

i have 3 forms: FormA ,FormB and FormC of which FormA is mdiParent form and form B and C are child forms. i write the following code in FormA load event.

private void frmMain_Load(object sender, EventArgs e)
{
   formB.MdiParent = this; //formB is instance of FormB             
   formC.MdiParent = this; //formC is instance of FormC
   formB.Show();      
}

我想要的是当我单击 FormB 上的按钮时,应显示 FormC.现在要做到这一点,我需要在 FormB 中按钮的单击事件中创建另一个 FormC 实例,还是我应该能够使用在 FormA 中创建的实例???

what i want is when i click a button on FormB, FormC should be shown. now to do this will i need to create another instance of FormC in click event of button in FormB or should i be able to use the instancce created in FormA???

如果需要创建一个单独的实例,那么有人可以解释这样做的原因吗?

if needed to create a separate instance then can someone plz explain the reason for doing so?

编辑- Oded 给出的答案很适合我.但是我可以将属性的返回类型设为 Form[] 以添加 1 个以上的引用,以便如果我想从 FormC 返回到 FormB,我可以使用类似的方法吗?

edit- the answer given by Oded suits me fine. but can i make the return type of the property as Form[] in order to add more than 1 references so that if i want to go back from FormC to FormB i can use similar method?

另外,如果我想将一些数据从 FormB 传递到 FormC,那我该怎么做?

also if i want to pass some data from FormB to FormC then how can i do that?

推荐答案

你的 FormB 需要对 FormC 的引用.

Your FormB needs a referenct to FormC.

您可以在 FromB 上添加属性来执行此操作:

You can add a property on your FromB to do this:

public Form FormCRef {get;set;}

然后在您的主要表单中:

Then in your main form:

formB.FormCRef = formC;

在您的 FormB 类中,在您的事件处理程序中执行此操作:

And in your FormB class do this in your event handler:

FormCRef.Show();

这篇关于在 C# 中从另一个子窗体调用一个子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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