C#

10.C#_WinForm - DialogResult (다른폼 값 넘기기)

Godffs 2009. 8. 24. 18:13
반응형

속성값을 이용해서 부모폼(Form1)과 자식폼(Form2)에서 값을 서로 넘기는 예제 입니다.


FrmResul.Designer.cs 와 FrmResultChild.Designer.cs [공통]

[그림10-1]

         private System.Windows.Forms.TextBox txtResult;
        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.TextBox txtParent;

1. 부모폼 txtParent.Text 값 입력 -> 전송 -> 자식폼 txtChild.Text 안에 값 출력
2. 자식폼 txtReturn.Text 값 입력 -> 확인 -> 부모폼 txtResult.Text 안에 값 출력 -> 폼닫기


FrmResult.cs [부모폼]
//속성
public string Value { get; set; }

private void btnOK_Click(object sender, EventArgs e)
{
        //자식 폼으로 데이터 전송
        WinFrmMain.Class.FrmDialogResultChild frmc = new
                FrmDialogResultChild();

        frmc.Owner = this; //자식폼 : FrmDialogResult //Owner이용 값 전달

        frmc.SendValue = txtParent.Text; //속성으로 값을 전달

        if (frmc.ShowDialog() == DialogResult.OK)
        {
                this.txtResult.Text = Value;
        }
}

FrmResultChild.cs [자식폼]

//속성
public string SendValue { get; set; } //부모에서 전송된 문자열을 받기위해서

private void FrmDialogResultChild_Load(object sender, EventArgs e)
{
        //폼 로드시 SendValue 속성에 담긴 값 저장
        this.txtChild.Text = SendValue;
}

private void btnsund_Click(object sender, EventArgs e)
{
        FrmDialogResult fdr = (FrmDialogResult)Owner;
        fdr.Value = txtReturn.Text; //텍스트 전송
        this.Close(); //현재 폼 닫기
}

결과확인

[그림10-2]


반응형