101. DialogResult (부모폼과 자식폼의 값 전달)
==> MainForm.cs
private void miDialogResult_Click(object sender, EventArgs e)
{
MyWinForms.Class.FrmDialogResult fdr = new MyWinForms.Class.FrmDialogResult();
fdr.Show();
}
==============================================================================================
==> FrmDialogResult.cs
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 MyWinForms.Class
{
public partial class FrmDialogResult : Form
{
public FrmDialogResult()
{
InitializeComponent();
}
// 속성
public string Value { get; set; }
private void btnSend_Click(object sender, EventArgs e)
{
// 자식 폼으로 데이터 전송
MyWinForms.Class.FrmDialogResultChild c = new FrmDialogResultChild();
c.Owner = this; // 자식 폼의 주인 FrmDialogResult
c.SendValue = txtParent.Text; // 속성으로 값을 전달
//DialogResult dr = c.ShowDialog(); // 폼 로드되면서 전송된 텍스트가 자식 폼에 출력
if (c.ShowDialog() == DialogResult.OK)
{
this.txtResult.Text = Value;
}
}
}
}
==============================================================================================
==> FrmDialogResultChild.cs
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 MyWinForms.Class
{
public partial class FrmDialogResultChild : Form
{
public FrmDialogResultChild()
{
InitializeComponent();
}
// 속성
public string SendValue { get; set; }
private void FrmDialogResultChild_Load(object sender, EventArgs e)
{
// 폼 로드시 SendValue 속성에 담긴 값 저장
this.txtChild.Text = SendValue;
}
private void btnOK_Click(object sender, EventArgs e)
{
FrmDialogResult fdr = (FrmDialogResult)Owner;
fdr.Value = txtReturn.Text; // 텍스트 전송
this.Close(); // 현재 폼 닫기
}
}
}
< 실행결과 >
==> FrmDialogResult(부모)에서 'abc'를 입력후 '전송'버튼을 누르면 FrmDialogResultChild(자식)에 같은값(abc)이 나옴
==> FrmDialogResultChild에서 '123'을 입력 후 '확인'버튼을 누르면 FrmDialogResultChild폼은 사라지면서 FrmDialogResult에서 '123'이 나옴