728x90
1. 강의 개요
이번 강의에서는 Windows Forms에서 **사용자 정의 컨트롤(Custom Control)**을 만들어 UI를 재사용 가능하고 확장성 있는 형태로 개발하는 방법을 학습합니다.
- 사용자 정의 컨트롤을 만들어 복잡한 기능을 캡슐화.
- 재사용 가능한 컴포넌트로 프로젝트의 생산성 향상.
2. 학습 목표
- 사용자 정의 컨트롤을 설계하고 폼에 추가.
- 속성(Property)과 메서드(Method)를 사용자 정의 컨트롤에 구현.
- 이벤트(Event)를 정의하고 폼에서 처리.
3. 사용자 정의 컨트롤이란?
**사용자 정의 컨트롤(Custom Control)**은 기본 Windows Forms 컨트롤(Button, TextBox 등)을 확장하거나, 여러 컨트롤을 조합하여 새로운 컨트롤을 생성하는 방식입니다.
4. 실습: 사용자 정의 컨트롤 만들기
요구사항
- "라벨 + 텍스트박스 + 버튼"으로 구성된 사용자 정의 컨트롤을 생성.
- 컨트롤에 텍스트를 입력한 후 버튼을 클릭하면 텍스트를 폼에 출력.
- LabelText 속성을 통해 라벨 텍스트를 외부에서 설정 가능.
구성 요소
구성 요소 이름 설명
Label | lblTitle | 사용자 정의 컨트롤의 라벨. |
TextBox | txtInput | 사용자 입력을 받을 텍스트박스. |
Button | btnSubmit | 텍스트박스 내용을 처리할 버튼. |
5. 사용자 정의 컨트롤 만들기
1. 사용자 정의 컨트롤 클래스 생성
Visual Studio에서 새 사용자 컨트롤 추가하기:
- 솔루션 탐색기 → 프로젝트 이름 오른쪽 클릭 → 추가 → 사용자 컨트롤 선택.
- 파일 이름을 CustomInputControl.cs로 지정.
CustomInputControl.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class CustomInputControl : UserControl
{
public CustomInputControl()
{
InitializeComponent();
btnSubmit.Click += BtnSubmit_Click;
}
// LabelText 속성
public string LabelText
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
// 이벤트 정의
public event EventHandler<string> SubmitClicked;
private void BtnSubmit_Click(object sender, EventArgs e)
{
// 텍스트박스 내용을 이벤트로 전달
SubmitClicked?.Invoke(this, txtInput.Text);
}
}
}
CustomInputControl.Designer.cs
namespace WindowsFormsApp1
{
partial class CustomInputControl
{
private System.ComponentModel.IContainer components = null;
private Label lblTitle;
private TextBox txtInput;
private Button btnSubmit;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.lblTitle = new Label();
this.txtInput = new TextBox();
this.btnSubmit = new Button();
this.SuspendLayout();
// Label
this.lblTitle.AutoSize = true;
this.lblTitle.Location = new System.Drawing.Point(10, 10);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(80, 20);
this.lblTitle.TabIndex = 0;
this.lblTitle.Text = "Label Title";
// TextBox
this.txtInput.Location = new System.Drawing.Point(100, 7);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(200, 27);
this.txtInput.TabIndex = 1;
// Button
this.btnSubmit.Location = new System.Drawing.Point(310, 7);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(75, 27);
this.btnSubmit.TabIndex = 2;
this.btnSubmit.Text = "Submit";
this.btnSubmit.UseVisualStyleBackColor = true;
// CustomInputControl
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.lblTitle);
this.Name = "CustomInputControl";
this.Size = new System.Drawing.Size(400, 40);
this.ResumeLayout(false);
this.PerformLayout();
}
}
}
2. 사용자 정의 컨트롤을 폼에 추가
Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeCustomControl();
}
private void InitializeCustomControl()
{
CustomInputControl customInputControl = new CustomInputControl
{
Location = new System.Drawing.Point(10, 10),
LabelText = "이름:"
};
customInputControl.SubmitClicked += CustomInputControl_SubmitClicked;
this.Controls.Add(customInputControl);
}
private void CustomInputControl_SubmitClicked(object sender, string inputText)
{
MessageBox.Show($"입력된 텍스트: {inputText}", "알림");
}
}
}
3. 폼 디자이너 코드
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.SuspendLayout();
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(500, 300);
this.Name = "Form1";
this.Text = "사용자 정의 컨트롤 예제";
this.ResumeLayout(false);
}
}
}
6. 실행 결과
- 사용자 정의 컨트롤
- 폼에 "이름:", 텍스트박스, 버튼이 포함된 사용자 정의 컨트롤이 추가됩니다.
- 텍스트 입력 후 버튼 클릭
- 텍스트박스에 값을 입력하고 "Submit" 버튼을 클릭하면 입력한 텍스트가 메시지박스로 표시됩니다.
- 예:
입력된 텍스트: 홍길동
7. 주요 개념 요약
- **사용자 정의 컨트롤(Custom Control)**은 UI를 캡슐화하고 재사용 가능하게 만들어 생산성을 높입니다.
- 속성(Property), 메서드(Method), 이벤트(Event)를 추가해 컨트롤의 동작을 확장할 수 있습니다.
- 사용자 정의 컨트롤을 활용하면 복잡한 기능을 간단히 구현하고 유지보수성을 높일 수 있습니다.
728x90
'Study > C#' 카테고리의 다른 글
C# Windows Forms 강의 17편: 파일 입출력(File I/O)과 OpenFileDialog, SaveFileDialog (0) | 2025.02.20 |
---|---|
C# Windows Forms 강의 16편: Drag & Drop (드래그 앤 드롭) 기능 구현 (0) | 2025.02.19 |
C# Windows Forms 강의 14편: SplitContainer와 TabControl로 동적 레이아웃 구성 (0) | 2025.02.17 |
C# Windows Forms 강의 13편: Chart 컨트롤로 데이터 시각화 (0) | 2025.02.16 |
C# Windows Forms 강의 12편: DataGridView와 ADO.NET (0) | 2025.02.15 |