如何在窗体应用程序的控件中嵌套控件
问题描述:
我想在另一个窗口中嵌入一个窗体应用程序。我可以在form1中嵌入form2进行一级嵌入。但是我想在form2中嵌入第三种形式,例如form3(它本身嵌入在form1中)。当我点击form2菜单条项时,它什么也没做(可能因为form3嵌入其中?)。
我该如何解决这个问题?
我尝试过:
I want to embed a window form app within another. I can do a one level embed from say embedding form2 in form1. However I want to embed a third form say form3 within form2(which is itself is embedded within form1). When i click the form2 menu strip item it does nothing(perhaps because form3 is embedded within it ?).
How can I solve this please?
What I have tried:
var f2 = new Form2();//Within Form1
f2.TopLevel = false;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Size = this.Size;
f2.BringToFront();
f2.Visible = true;
this.Controls.Add(f2);
var f3 = new Form3();//Within form2
f3.TopLevel = false;
f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f3.Size = this.Size;
f3.BringToFront();
f3.Visible = true;
this.Controls.Add(f3);
答
您显示的代码没有在Form2中嵌入Form3 - 它将它与Form2一起嵌入Form1中。如果我简化你的代码:
The code you show doesn't embed Form3 inside Form2 - it embeds it in Form1 alongside Form2. If I simplify your code:
var f2 = new Form2();
...
this.Controls.Add(f2);
var f3 = new Form3();
...
this.Controls.Add(f3);
您需要做的是:
What you would need to do is:
var f2 = new Form2();
...
this.Controls.Add(f2);
var f3 = new Form3();
...
f2.Controls.Add(f3);