C# 中 mdi 父级中的子窗体 [英] Child form in a mdi parent in C#
本文介绍了C# 中 mdi 父级中的子窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码在 MDI 父窗体中显示子窗体.如您所知,单击该按钮将导致出现一个新表单.继续单击按钮,您的屏幕将充满空白表格.为了阻止这种情况的发生,我将在按钮之外创建表单的代码移动了.
I use the following codes for showing child form in a MDI parent form. As you know clicking the button will cause a new form to appear. Keep clicking the button and your screen will be filled with blank forms. To stop this from happening, I moved the code that creates the form outside of the button.
像这样:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 f2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
f2.MdiParent = this;
f2.Show();
}
但是当我关闭子表单并想再次打开它时,它不会让我.
But when I close the child form and want to open it again, it won't let me.
请帮我解决这个问题.
推荐答案
您需要跟踪表单状态,以便知道您需要创建一个新表单.像这样:
You need to keep track of the form state so you know you need to create a new one. Like this:
private Form2 f2;
private void button1_Click(object sender, EventArgs e)
{
if (f2 == null) {
f2 = new Form2();
f2.MdiParent = this;
f2.FormClosed += delegate { f2 = null; };
f2.Show();
}
else {
f2.Activate();
}
}
这篇关于C# 中 mdi 父级中的子窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文