在 C# 中的多个表单之间导航

发布于 2024-11-29 04:20:42 字数 700 浏览 0 评论 0原文

我有 2 种形式,其中一种添加了冲击波组件,它播放 Flash 影片,单击在 Flash 中创建的按钮时,形式 1 (frmFlashIntro) 卸载,形式 2(frmMain) 启动。 因为 frmFlashIntro 是一个表单,仅在我想卸载表单而不是隐藏它时使用。我还希望 frmFlashIntro 关闭后 frmMain 能够完全控制,就像它是主窗体一样。 隐藏表单似乎不是做这件事的好方法。

我目前正在做的是我在 frmFlashIntro 中创建了一个属性,并向它们添加了 get 和 set 方法(它们都是空白的)

public Form FormfrmMainRef { get; set; }

我将此代码添加到了单击事件中。

frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
FormfrmMainRef.Show();

现在这有效地显示了我的 frmMain 但保持了 frmFlashIntro 也运行所以我这样做了

frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also

我知道我在最后一步做错了。谁能告诉我如何关闭该表单并释放它所持有的资源。

我也不想使用 MDI

I have 2 forms of which one of them has a shockwave component added to it, it plays a flash movie and on click of a button created in flash the form 1 (frmFlashIntro) unloads and form 2(frmMain) starts.
since frmFlashIntro is a form which is used only once i want to unload the form rather than hide it. i also want frmMain to have complete control once frmFlashIntro closes as if it is the main form.
Hiding the form does not seem to be a good way to go about doing this thing.

What i am currently doing is i created a property in frmFlashIntro and added get and set methods to them (both of them are blanK)

public Form FormfrmMainRef { get; set; }

I added this code to a click event.

frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
FormfrmMainRef.Show();

now this effectively shows my frmMain but keeps the frmFlashIntro also running so i did this

frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also

i know i am doing something wrong in the last step. Can anyone please tell me how do i close that form and free the resources held by it.

Also i do not want to use an MDI

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩心态 2024-12-06 04:20:42

看起来 frmMain 没有引用原始的 frmFlashIntro。您的代码

frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also

实际上将创建第二个 frmFlashIntro,因此当您调用 fi.Close() 时,您实际上是在关闭第二个表单,而不是原始表单。

理论上,您应该能够向 frmMain 添加一个属性,该属性是对 frmFlashIntro 的引用,然后对其调用 Dispose,例如

frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
fm.FormIntroRef = this;
FormfrmMainRef.Show();

然后在 frmMain 的 load 事件中,添加对 FormIntroRef.close() 的调用

好吧,但是,除此之外,我认为解决此问题的最佳方法是更新 Program.cs 文件,然后有类似的内容

frmFlashIntro flashIntro = new frmFlashIntro
frmFlashIntro.ShowDialog();
frmMain mainForm = new frmMain()
Application.Run(frmMain);

哦,第二个旁注,通常,类型以大写字母开头,这些类型的实例以小写字母开头,并且采用驼峰式大小写。所以你通常会得到类似的东西

MainForm frmMain = new MainForm()

,这样就可以达到你想要的结果。

It looks like frmMain has no reference to the original frmFlashIntro. Your bit of code

frmFlashIntro fi=new frmFlashIntro();
fi.Close();//checked with dispose also

will actually create a second frmFlashIntro, so when you call fi.Close() you're actually closing the second form, not the original.

Theoretically you should be able to add a property to your frmMain that's a reference to frmFlashIntro, and then call Dispose on that so something like

frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
fm.FormIntroRef = this;
FormfrmMainRef.Show();

And then in the load event of frmMain, add a call to FormIntroRef.close()

Ok, but, all that aside, I think the best way to solve this problem is to update the Program.cs file and then have something like

frmFlashIntro flashIntro = new frmFlashIntro
frmFlashIntro.ShowDialog();
frmMain mainForm = new frmMain()
Application.Run(frmMain);

Oh, and a second side note, generally, types begin with capital letters, and instances of those types begin lowercase, and the are camel cased. So you'd normally have something like

MainForm frmMain = new MainForm()

That should achieve your desired result.

半枫 2024-12-06 04:20:42

您是否尝试使用 fi.Dispose() 处理表单?

Did you try to dispose the form with fi.Dispose() ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文