🔎 유용한 정보
1. 강의 개요
이번 강의에서는 ProgressBar 컨트롤을 사용하여 작업의 진행 상태를 시각적으로 표시하는 방법을 학습합니다.
ProgressBar는 파일 다운로드, 처리 작업 등 시간 소요 작업의 상태를 사용자에게 제공하는 데 유용합니다.
2. 학습 목표
- ProgressBar의 동작 원리를 이해하고 기본 사용법 익히기.
- Timer와 ProgressBar를 연동하여 작업 진행률 시뮬레이션.
- 작업 완료 시 메시지 표시.
3. ProgressBar란?
ProgressBar는 작업의 진행 상태를 시각적으로 표현하는 Windows Forms 컨트롤입니다.
- Minimum: 진행률의 시작 값.
- Maximum: 진행률의 최대 값.
- Value: 현재 진행률 값.
ProgressBar 주요 속성
속성 설명 예제
Minimum | 진행률의 최소 값 | progressBar1.Minimum = 0; |
Maximum | 진행률의 최대 값 | progressBar1.Maximum = 100; |
Value | 현재 진행률 값 | progressBar1.Value = 50; |
Style | 진행 표시 스타일 (Continuous, Blocks 등) | progressBar1.Style = ProgressBarStyle.Blocks; |
ProgressBar 주요 이벤트
ProgressBar는 이벤트를 직접 제공하지 않지만, 외부 작업(Timer, BackgroundWorker 등)과 연동하여 진행률을 갱신합니다.
4. 실습: ProgressBar를 활용한 작업 진행률 표시
요구사항
- ProgressBar를 0%부터 100%까지 채우는 작업 구현.
- Timer를 사용해 일정 간격으로 진행률 증가.
- 작업 완료 시 메시지 박스를 통해 완료 알림.
폼 구성
컨트롤 타입 이름 텍스트 위치 크기
ProgressBar | progressBar1 | (없음) | 폼 상단 중앙 | (300 x 30) |
Button | btnStart | "작업 시작" | 폼 하단 중앙 | (100 x 30) |
코드 작성
Form1.cs
using System;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Timer timer1;
private int progressValue;
public Form1()
{
InitializeComponent();
InitializeProgressBar();
}
private void InitializeProgressBar()
{
// ProgressBar 설정
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 0;
// Timer 설정
timer1 = new Timer
{
Interval = 100 // 100ms 간격
};
timer1.Tick += Timer1_Tick;
// "작업 시작" 버튼 클릭 이벤트 연결
btnStart.Click += BtnStart_Click;
}
// Timer Tick 이벤트: 진행률 증가
private void Timer1_Tick(object sender, EventArgs e)
{
progressValue += 5; // 진행률 증가
if (progressValue <= progressBar1.Maximum)
{
progressBar1.Value = progressValue;
}
else
{
timer1.Stop(); // 작업 완료 시 타이머 중지
MessageBox.Show("작업이 완료되었습니다!", "완료");
}
}
// "작업 시작" 버튼 클릭 이벤트
private void BtnStart_Click(object sender, EventArgs e)
{
if (!timer1.Enabled)
{
progressValue = 0; // 진행률 초기화
progressBar1.Value = 0;
timer1.Start(); // 타이머 시작
}
}
}
}
Form1.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private ProgressBar progressBar1;
private Button btnStart;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.progressBar1 = new ProgressBar();
this.btnStart = new Button();
this.SuspendLayout();
// progressBar1
this.progressBar1.Location = new System.Drawing.Point(50, 50);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(300, 30);
this.progressBar1.TabIndex = 0;
// btnStart
this.btnStart.Location = new System.Drawing.Point(150, 100);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(100, 30);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "작업 시작";
this.btnStart.UseVisualStyleBackColor = true;
// Form1
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(400, 200);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.progressBar1);
this.Name = "Form1";
this.Text = "ProgressBar 예제";
this.ResumeLayout(false);
}
}
}
5. 실행 결과
- 초기 상태
- ProgressBar는 비어 있고, "작업 시작" 버튼이 활성화되어 있습니다.
- 작업 진행
- "작업 시작" 버튼 클릭 → ProgressBar가 5%씩 증가하며, 작업 진행 상태를 표시합니다.
- 작업 완료
- ProgressBar가 100%에 도달하면 Timer가 중지되고, 완료 메시지가 표시됩니다.
6. 주요 개념 요약
- ProgressBar 구성 요소
- Minimum, Maximum, Value를 설정하여 진행 상태를 제어.
- Timer와의 연동
- Timer를 사용해 일정 간격으로 ProgressBar의 진행률을 업데이트.
- 작업 완료 처리
- ProgressBar의 Value가 Maximum에 도달하면 타이머를 중지하고 작업 완료 처리를 실행.
'📁 [4] 개발자 정보 & 코드 노트 > C#' 카테고리의 다른 글
C# Windows Forms 강의 42편: UserControl로 복잡한 UI 구성 (1) | 2025.03.17 |
---|---|
C# Windows Forms 강의 41편: Custom Control 제작 (0) | 2025.03.16 |
C# Windows Forms 강의 39편: NotifyIcon과 ContextMenuStrip을 활용한 트레이 아이콘 구현 (0) | 2025.03.14 |
C# Windows Forms 강의 38편: TreeView로 계층적 데이터 표시 (0) | 2025.03.13 |
C# Windows Forms 강의 37편: ListView를 활용한 데이터 목록 표시 (0) | 2025.03.12 |
🔎 유용한 정보