반응형
C# WinForms 기반의 메모 관리 프로그램을 제작하겠습니다.
이 프로젝트는 C# .NET WinForms을 사용하며, JSON 파일을 활용한 메모 저장/불러오기 기능을 포함합니다.
🔹 기능 요구사항
- 메모 추가 – 사용자가 입력한 메모를 저장
- 메모 삭제 – 선택한 메모를 삭제
- 메모 목록 조회 – 저장된 메모를 리스트에서 확인
- 메모 저장 – JSON 파일(notes.json)에 저장
- 메모 불러오기 – 프로그램 실행 시 기존 메모 불러오기
📌 프로젝트 구조
MemoApp/
├── FormMain.cs (메인 폼 - UI)
├── NoteManager.cs (메모 데이터 관리)
├── Note.cs (메모 모델)
├── notes.json (메모 데이터 저장 파일)
├── Program.cs (애플리케이션 실행)
├── MemoApp.csproj (프로젝트 파일)
🖥 1. 프로젝트 생성
- Visual Studio에서 WinForms App (.NET) 프로젝트 생성
- 프로젝트 이름: MemoApp
🎨 2. UI 디자인
FormMain.cs (디자이너에서 추가할 UI 요소)
- ListBox lstNotes → 메모 목록 표시
- TextBox txtNote → 메모 입력 필드
- Button btnAdd → 메모 추가 버튼
- Button btnDelete → 메모 삭제 버튼
🔹 3. 코드 작성
📌 (1) 메모 모델 (Note.cs)
메모의 데이터를 정의하는 간단한 클래스입니다.
using System;
namespace MemoApp
{
public class Note
{
public string Content { get; set; }
public DateTime CreatedAt { get; set; }
public override string ToString()
{
return $"{CreatedAt:yyyy-MM-dd HH:mm} - {Content}";
}
}
}
📌 (2) 메모 저장 및 관리 (NoteManager.cs)
JSON 파일(notes.json)을 사용하여 메모를 저장/불러오는 기능을 구현합니다.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace MemoApp
{
public static class NoteManager
{
private static readonly string FilePath = "notes.json";
public static List<Note> LoadNotes()
{
if (!File.Exists(FilePath))
return new List<Note>();
string json = File.ReadAllText(FilePath);
return JsonSerializer.Deserialize<List<Note>>(json) ?? new List<Note>();
}
public static void SaveNotes(List<Note> notes)
{
string json = JsonSerializer.Serialize(notes, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(FilePath, json);
}
}
}
📌 (3) 메인 폼 (FormMain.cs)
사용자가 메모를 입력하고 관리하는 UI 로직입니다.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MemoApp
{
public partial class FormMain : Form
{
private List<Note> notes = new List<Note>();
public FormMain()
{
InitializeComponent();
LoadNotes();
}
private void LoadNotes()
{
notes = NoteManager.LoadNotes();
RefreshNoteList();
}
private void RefreshNoteList()
{
lstNotes.Items.Clear();
foreach (var note in notes)
lstNotes.Items.Add(note);
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtNote.Text))
{
MessageBox.Show("메모를 입력하세요!", "경고", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var newNote = new Note { Content = txtNote.Text, CreatedAt = DateTime.Now };
notes.Add(newNote);
NoteManager.SaveNotes(notes);
txtNote.Clear();
RefreshNoteList();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (lstNotes.SelectedIndex == -1)
{
MessageBox.Show("삭제할 메모를 선택하세요!", "경고", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
notes.RemoveAt(lstNotes.SelectedIndex);
NoteManager.SaveNotes(notes);
RefreshNoteList();
}
}
}
🎨 4. WinForms UI 추가
디자이너에서 아래 UI 요소 배치
- ListBox (lstNotes) – 메모 목록
- TextBox (txtNote) – 메모 입력창
- Button (btnAdd) – "추가" 버튼
- Button (btnDelete) – "삭제" 버튼폼의 Load 이벤트에 메모 불러오기 추가
private void FormMain_Load(object sender, EventArgs e)
{
LoadNotes();
}
✅ 5. 실행 결과
- 메모 입력 후 "추가" 버튼 클릭 → 메모 목록에 추가
- 메모 목록에서 항목을 선택 후 "삭제" 버튼 클릭 → 선택된 메모 삭제
- 프로그램 종료 후 다시 실행하면 이전 메모 목록 유지 (notes.json 저장)
🔹 6. 프로젝트 개선 가능 사항
✔ SQLite DB 사용 – JSON 대신 SQLite로 데이터 저장
✔ WPF로 UI 개선 – DataGrid 등 활용한 모던한 UI 적용
✔ 메모 검색 기능 추가 – 특정 단어로 메모 검색 기능
✔ 자동 저장 기능 추가 – 입력 즉시 저장
반응형