如何避免在C#windows窗体应用程序中打开表单的多个实例? [英] How to avoid opening multiple instance of a form in C# windows forms application?

查看:74
本文介绍了如何避免在C#windows窗体应用程序中打开表单的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我写了一个程序,里面有一个关于按钮,按下主窗体上的about按钮会打开另一个表格,提供一些关于程序,然而,单击关于按钮将使这种形式的另一个实例,我该如何避免它?



我的代码是:

Hello,

I have write a program which has an About button, pressing the about button on the main form will open another form which gives some information about the program, however, clicking the about button will make another instances of such form, how can I avoid it?

my code is:

private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
    FormAbout formAbout = new FormAbout();
    formAbout.Show();
}





提前致谢

AM



Thanks in advance
AM

推荐答案

最简单的方法是使用ShowDialog:

Simplest way is to use ShowDialog instead:
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
    FormAbout formAbout = new FormAbout();
    formAbout.ShowDialog();
}


我认为OriginalGriff在这里给了你一个正确,可靠的答案;



管理像这样的辅助表格的一个因素是你需要做多少昂贵(时间,记忆)工作,以便在显示之前用相关内容填充二级表格。



为什么不创建一次Form,填充其中一次的字段,然后重新使用Form ...而不是每次要显示次要表格的新实例时,是什么?
I think OriginalGriff has given you a correct, solid, answer here;.

A factor in how you manage a "secondary" Form like this is the extent to which you need to do possibly "expensive" (time, memory) work to fill that secondary Form with its relevant content before you show it.

Why not create the Form once, populate its fields that do not change once, and then re-use the Form ... rather than creating a new instance of the secondary Form each time you want to show it ?


这篇关于如何避免在C#windows窗体应用程序中打开表单的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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