[英]How to open child form with a button on another child form in side the mdi parent form
I have 3 forms MainForm, Child1 and Child2 . 我有3个表单MainForm,Child1和Child2。 MainForm is the mdi container.
MainForm是mdi容器。 At runtime child 1 is automatically loaded inside the MainForm.
在运行时,子项1将自动加载到MainForm中。 I am trying to open the form child2 from a button on child1 but i cannot seem to do it.
我正在尝试通过child1上的按钮打开child2表单,但我似乎无法做到。 I did everything i could.
我已尽力了。 Please help.
请帮忙。
I tried using the hide show and activate methods but nothing worked. 我尝试使用隐藏显示和激活方法,但没有任何效果。 The child1 and child2 forms are separately built.
child1和child2表单是分别构建的。
public partial class Form1 : Form
{
public child1 c1;
public child2 c2;
public void LoadChild2()
{
if (c2 == null)
{
c2 = new child2() ;
c2.MdiParent = this;
c2.FormClosed += c2_FormClosed;
c2.FormBorderStyle = FormBorderStyle.None;
c2.Dock = DockStyle.Fill;
c2.Show();
}
else { c2.Activate(); }
}
void c2_FormClosed(object sender, FormClosedEventArgs e)
{
c2 = null;
}
public void LoadChild1()
{
if (c1 == null)
{
c1 = new child1();
c1.MdiParent = this;
c1.FormClosed += c1_FormClosed;
c1.FormBorderStyle = FormBorderStyle.None;
c1.Dock = DockStyle.Fill;
c1.Show();
}
else { c1.Activate(); }
}
void c1_FormClosed(object sender, FormClosedEventArgs e)
{
c1 = null;
//throw new NotImplementedException();
}
public Form1()
{
InitializeComponent();
LoadChild1();
// c2 = new child2();
// c1 = new child1();
// c2.MdiParent = this;
// c1.MdiParent = this;
//c1.WindowState = FormWindowState.Maximized;
// c1.FormBorderStyle = FormBorderStyle.None;
//c1.Dock = DockStyle.Fill;
// c2.FormBorderStyle = FormBorderStyle.None;
//c2.Dock = DockStyle.Fill;
// c2.Show();
// c1.Show();
//c1.Activate();
}
public partial class child1 : Form
{
//public child2 c2;
public child1()
{
InitializeComponent();
}
private void child1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.LoadChild2();
//child2 c2 = new child2();
//Form1 f = new Form1();
//c2.FormBorderStyle = FormBorderStyle.None;
// c2.Dock = DockStyle.Fill;
//c2.MdiParent = f;
// c2.Activate();
// c2.BringToFront();
// this.Hide();
//this.Close();
}
}
public partial class child2 : Form
{
public child2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//this.Close();
Form1 f = new Form1();
f.LoadChild1();
// child1 c1 = new child1();
// c1.FormBorderStyle = FormBorderStyle.None;
// c1.Dock = DockStyle.Fill;
// c1.Parent = f;
// c1.Show();
// c1.Activate();
}
private void child2_Load(object sender, EventArgs e)
{
}
}
Make sure to set the MdiParent
property; 确保设置
MdiParent
属性; if spawning the new form from a Click
event handler of a button on an existing MdiChild, you can just set it to that child's MdiParent: 如果从现有MdiChild上的按钮的
Click
事件处理程序中生成新表单,则可以将其设置为该孩子的MdiParent:
// Event handler inside MdiChild form (Child1)
private void spawnNewChildButton_Click(object sender, EventArgs e)
{
// Child2 is a System.Windows.Forms.Form
var child2 = new Child2() { MdiParent = MdiParent };
child2.Show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.