윈폼(winform) MDI 부모 자식창 예제
MDI : Multi Document(Form) Interface
여태까지 했던건 싱글 폼(폼 하나에 폼하나만 있는것.)
부모창
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Start
{
public partial class MDI : Form
{
private int n;
public MDI()
{
InitializeComponent();
n = 1;
}
private void 새창열기ToolStripMenuItem_Click(object sender, EventArgs e)
{
MDIChild child = new MDIChild();
child.MdiParent = this;
child.Text = "자식" + n.ToString();
child.Show();
n++;
}
private void 모든자식창에접근하기ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form[] children = this.MdiChildren;
/* foreach로 한 구문과 똑같다.
for (int i = 0; i < children.Length; i++)
{
MDIChild childe = (MDIChild)children[i];
childe.richTextBox1.Text = "부모로부터 접근";
}
*/
foreach (MDIChild child in this.MdiChildren)
{
child.richTextBox1.Text = "부모접근";
}
}
private void 활성화된자식창에접근하기ToolStripMenuItem_Click(object sender, EventArgs e)
{
//1의 창
MDIChild child = (MDIChild)this.ActiveMdiChild;
child.Text = "활성화된 자식창";
}
private void 계단식ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void 수평정렬ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void 수직정렬ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileVertical);
}
private void 아이콘배열ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.ArrangeIcons);
}
}
}
자식창
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Start
{
public partial class MDIChild : Form
{
public MDIChild()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//MDI 부모 접근
//child.MdiParent = this;
//MDI parent = (MDI)this.Owner;
MDI parent = (MDI)this.MdiParent;
parent.Text = this.Text + "자식으로부터 접근";
}
}
}