关于一次比赛形式 [英] Regarding diplaying form once

查看:102
本文介绍了关于一次比赛形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个菜单,其中有菜单项.单击这些菜单项后,我将显示放置在单独表单上的网格,图表.现在,一旦我打开一个图表/网格,我就不想在菜单项单击时再次打开该图表/网格,即我只希望它们一次打开(请记住,我想同时打开一个图表和一个网格表单,这排除frm.ShowDialog()选项).

我的问题是如何限制应用程序仅显示一次表单.我可以有两种不同的形式,但不能完全相同.

请帮我.我的应用程序是基于Windows的应用程序

谢谢

Hello,

I have a menu in which there are menu items. On the click of these menu items, I am displaying grids, charts which are placed on separate forms. Now once I open a chart/Grid, I donot want the chart/Grid to open again on menu item click, i.e I just want them to open once(keep in mind, I want to open a chart and a grid form simultaneously, this rules out the frm.ShowDialog()option).

My question is how do I restrict the application to display the forms just once. I can have two separate forms but not the same one.

Please help me on this. MY application is a windows based application

Thanks

推荐答案

我认为您在表单类和实例之间存在混淆.当您说某事显示为两次"时,可能仅意味着您同时拥有同一类的两个实例.如果需要避免这种情况,则应仅使用一个实例.有很多方法可以做到这一点,但是最好和最通用的方法是使用 lazy 模式,它是最简单的形式:
I think you have a confusion between form classes and instances. When you say that something is shown "twice", it may only mean that you have two instances of the same class at the same time. If you need to avoid it, you should work with only one instance. There are a number of ways to do it, but the best and most universal approach here is using the lazy pattern, in it simplest form:
partial class MyMainForm {

    MyChartForm chartForm; // at first, it's null; will remain null before first use

    void ShowChart() { //add "internal" if you want to use it from outside the form
       if (chartForm == null) {
           chartForm = new MyChartForm();
           chartForm.Owner = this;
           chartForm.ShowInTaskbar = false; // important for application integrity
       } //if chartForm == null
       chartForm.ShowDialog();
    } //ShowChart

} //class MyMainForm


这种方法的一个好处是,当某些操作无效时,您不必冒过早创建表单的风险.对于System.Windows.Forms来说,这不是什么大问题,但是使用WPF,它可以真正节省一天的时间.

如果您要使用非模态附加形式,则此技术将需要其他条件. 当使用Form.Show方法而不是Form.ShowDialog时,会出现这种情况. [END EDIT]

在这种情况下,我建议您防止关闭(因为由于放置了对象而无法再次显示它;您需要将其隐藏在FormClosing上并取消关闭(可以取消此事件;该事件参数类型具有Cancel布尔成员;您需要将其设置为true),以便稍后再次显示该表单.

另请参见:
http://msdn.microsoft.com/en-us/library/system.componentmodel. canceleventargs.aspx [ ^ ],
http://en.wikipedia.org/wiki/Lazy_initialization [


One benefit of this approach is that you are not risking of creating a form too early, when some of the operations would be invalid. For System.Windows.Forms, this is not a big problem, but with WPF, it can really save a day.

This technique will require something else if you want to have a non-modal additional form. Such situation appears when Form.Show method is used instead of Form.ShowDialog. [END EDIT]

In this case, I would advise you to prevent closing (as you won''t be able to show it again because of disposed objects; you need to hide it on FormClosing and cancel closing (this event can be canceled; the event argument type has Cancel Boolean member; you will need to set it to true), to show the form again later.

Please see also:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.aspx[^],
http://en.wikipedia.org/wiki/Lazy_initialization[^].

—SA


这篇关于关于一次比赛形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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