在 C# 中的多个表单之间导航
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 frmMain 没有引用原始的 frmFlashIntro。您的代码
实际上将创建第二个 frmFlashIntro,因此当您调用 fi.Close() 时,您实际上是在关闭第二个表单,而不是原始表单。
理论上,您应该能够向 frmMain 添加一个属性,该属性是对 frmFlashIntro 的引用,然后对其调用 Dispose,例如
frmMain fm = new frmMain();
this.FormfrmMainRef = fm;
fm.FormIntroRef = this;
FormfrmMainRef.Show();
然后在 frmMain 的 load 事件中,添加对 FormIntroRef.close() 的调用
好吧,但是,除此之外,我认为解决此问题的最佳方法是更新 Program.cs 文件,然后有类似的内容
哦,第二个旁注,通常,类型以大写字母开头,这些类型的实例以小写字母开头,并且采用驼峰式大小写。所以你通常会得到类似的东西
,这样就可以达到你想要的结果。
It looks like frmMain has no reference to the original frmFlashIntro. Your bit of code
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
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
That should achieve your desired result.
您是否尝试使用 fi.Dispose() 处理表单?
Did you try to dispose the form with fi.Dispose() ?