반응형
1. 강의 개요
이번 강의에서는 폼 간 데이터 전달 방법을 학습합니다.
Windows Forms에서 여러 폼을 사용할 때, 데이터 전달은 흔히 발생하는 작업입니다.
예를 들어, 부모 폼에서 자식 폼으로 데이터 전달, 자식 폼에서 부모 폼으로 데이터 반환 등의 작업을 구현합니다.
2. 학습 목표
- 부모 → 자식 폼으로 데이터 전달.
- 자식 → 부모 폼으로 데이터 반환.
- 이벤트와 델리게이트를 활용한 폼 간 데이터 전달.
3. 폼 간 데이터 전달 방법
1. 부모 → 자식 폼
부모 폼에서 자식 폼 객체를 생성하고, 자식 폼의 속성이나 생성자를 통해 데이터를 전달합니다.
2. 자식 → 부모 폼
자식 폼에서 데이터를 부모 폼으로 전달하려면 다음 중 하나를 사용할 수 있습니다:
- 자식 폼의 Public 속성을 통해 부모가 데이터에 접근.
- 이벤트와 델리게이트를 사용해 데이터 전달.
4. 실습: 폼 간 데이터 전달 구현
요구사항
- 부모 폼에서 텍스트 입력 데이터를 자식 폼으로 전달.
- 자식 폼에서 데이터를 수정 후 부모 폼으로 반환.
- 이벤트와 델리게이트를 활용해 데이터를 부모 폼으로 전달.
폼 구성
부모 폼
컨트롤 타입 이름 텍스트 위치 크기
TextBox | txtParentData | (빈 상태) | 중앙 위쪽 | (200 x 30) |
Button | btnOpenChild | "자식 폼 열기" | 중앙 아래쪽 | (100 x 30) |
자식 폼
컨트롤 타입 이름 텍스트 위치 크기
TextBox | txtChildData | (빈 상태) | 중앙 위쪽 | (200 x 30) |
Button | btnSendToParent | "부모로 데이터 보내기" | 중앙 아래쪽 | (150 x 30) |
코드 작성
1. 자식 폼 코드 (ChildForm.cs)
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class ChildForm : Form
{
// 부모 폼에 데이터 전달을 위한 델리게이트 정의
public delegate void DataSentHandler(string data);
public event DataSentHandler OnDataSent;
public string ChildData
{
get => txtChildData.Text;
set => txtChildData.Text = value;
}
public ChildForm()
{
InitializeComponent();
btnSendToParent.Click += BtnSendToParent_Click;
}
// "부모로 데이터 보내기" 버튼 클릭 이벤트
private void BtnSendToParent_Click(object sender, EventArgs e)
{
OnDataSent?.Invoke(txtChildData.Text); // 부모로 데이터 전달
this.Close(); // 자식 폼 닫기
}
}
}
2. 자식 폼 디자이너 코드 (ChildForm.Designer.cs)
namespace WindowsFormsApp1
{
partial class ChildForm
{
private System.ComponentModel.IContainer components = null;
private TextBox txtChildData;
private Button btnSendToParent;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.txtChildData = new TextBox();
this.btnSendToParent = new Button();
this.SuspendLayout();
// txtChildData
this.txtChildData.Location = new System.Drawing.Point(20, 20);
this.txtChildData.Name = "txtChildData";
this.txtChildData.Size = new System.Drawing.Size(200, 27);
this.txtChildData.TabIndex = 0;
// btnSendToParent
this.btnSendToParent.Location = new System.Drawing.Point(20, 60);
this.btnSendToParent.Name = "btnSendToParent";
this.btnSendToParent.Size = new System.Drawing.Size(150, 30);
this.btnSendToParent.TabIndex = 1;
this.btnSendToParent.Text = "부모로 데이터 보내기";
this.btnSendToParent.UseVisualStyleBackColor = true;
// ChildForm
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(300, 120);
this.Controls.Add(this.btnSendToParent);
this.Controls.Add(this.txtChildData);
this.Name = "ChildForm";
this.Text = "자식 폼";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
3. 부모 폼 코드 (Form1.cs)
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeEvents();
}
private void InitializeEvents()
{
btnOpenChild.Click += BtnOpenChild_Click;
}
// "자식 폼 열기" 버튼 클릭 이벤트
private void BtnOpenChild_Click(object sender, EventArgs e)
{
ChildForm childForm = new ChildForm();
// 부모에서 자식으로 데이터 전달
childForm.ChildData = txtParentData.Text;
// 자식 폼에서 데이터 반환 이벤트 처리
childForm.OnDataSent += ChildForm_OnDataSent;
childForm.ShowDialog(); // 자식 폼 열기
}
// 자식 폼에서 데이터 반환 이벤트 처리
private void ChildForm_OnDataSent(string data)
{
txtParentData.Text = data; // 반환된 데이터를 부모 폼의 TextBox에 표시
}
}
}
4. 부모 폼 디자이너 코드 (Form1.Designer.cs)
namespace WindowsFormsApp1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private TextBox txtParentData;
private Button btnOpenChild;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.txtParentData = new TextBox();
this.btnOpenChild = new Button();
this.SuspendLayout();
// txtParentData
this.txtParentData.Location = new System.Drawing.Point(20, 20);
this.txtParentData.Name = "txtParentData";
this.txtParentData.Size = new System.Drawing.Size(200, 27);
this.txtParentData.TabIndex = 0;
// btnOpenChild
this.btnOpenChild.Location = new System.Drawing.Point(20, 60);
this.btnOpenChild.Name = "btnOpenChild";
this.btnOpenChild.Size = new System.Drawing.Size(100, 30);
this.btnOpenChild.TabIndex = 1;
this.btnOpenChild.Text = "자식 폼 열기";
this.btnOpenChild.UseVisualStyleBackColor = true;
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(300, 120);
this.Controls.Add(this.btnOpenChild);
this.Controls.Add(this.txtParentData);
this.Name = "Form1";
this.Text = "부모 폼";
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
5. 실행 결과
- 부모 → 자식 데이터 전달
- 부모 폼에서 텍스트를 입력 후 "자식 폼 열기" 클릭 → 자식 폼의 TextBox에 전달된 데이터 표시.
- 자식 → 부모 데이터 반환
- 자식 폼에서 데이터를 수정 후 "부모로 데이터 보내기" 클릭 → 수정된 데이터가 부모 폼의 TextBox에 표시.
6. 주요 개념 요약
- 부모 → 자식 데이터 전달
- 자식 폼의 속성을 사용해 데이터를 전달.
- 자식 → 부모 데이터 반환
- 델리게이트와 이벤트를 사용해 데이터를 부모 폼으로 전달.
- 폼 간의 데이터 공유
- 부모와 자식 폼 간의 데이터를 안전하게 주고받는 구조 설계 가능.
반응형
'📁 [4] 개발자 정보 & 코드 노트 > C#' 카테고리의 다른 글
C# Windows Forms 강의 49편: 데이터 바인딩과 DataGridView 고급 사용 (0) | 2025.03.24 |
---|---|
C# Windows Forms 강의 48편: 리소스 관리 (이미지, 아이콘, 사운드 파일) (0) | 2025.03.23 |
C# Windows Forms 강의 46편: BackgroundWorker로 비동기 작업 처리 (0) | 2025.03.21 |
C# Windows Forms 강의 45편: OpenFileDialog와 SaveFileDialog 고급 사용 (0) | 2025.03.20 |
C# Windows Forms 강의 44편: Timer와 GDI+를 활용한 간단한 애니메이션 효과 (0) | 2025.03.19 |