학생조회프로그램 - 외부클래스 분리버전

2025. 11. 5. 13:52·C#

StudentApp.sln
0.00MB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO; // 파일스트림

namespace StudentApp
{
    public partial class Form1 : Form
    {
        // 필드
        private List<Student> students = new List<Student>(); // 학생 리스트 (이것도 어디엔가 분리돼야함)
        private IniFile iniFile = new IniFile(); // 공용으로 한 번만 생성
        private Create create = new Create();
        private ListManage listManage = new ListManage();


        public Form1()
        {
            InitializeComponent();
        }

        // 학생 생성
        private void btnCreate_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;

            int age; // TryParse 라는 함수도 있음
            try
            {
                age = int.Parse(txtAge.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show("나이는 숫자로 입력해주세요.");
                return;
            }

            string major = txtMajor.Text;

            // 객체 생성
            Student student1 = create.CreateStudent(name, age, major);

            // 결과 출력
            lbResult.Text = student1.Introduce();

            // 학생 리스트에 추가
            listManage.AddStudent(students, student1);

            for (int i = 0; i < students.Count; i++)
            {
                listStudents.Items.Add(students[i].Name);
            }
        }

        // 파일 저장
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                iniFile.WriteFile(students);
                MessageBox.Show("학생 정보가 저장되었습니다.");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"파일 저장 실패: {ex.Message}");
            }
        }

        // 파일 읽기
        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                students = iniFile.ReadFile();

                listStudents.Items.Clear(); // 리스트박스 초기화
                foreach (var s in students) // 학생 이름들 추가
                {
                    listStudents.Items.Add(s.Name);
                }

                MessageBox.Show("불러오기 완료");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"파일 읽기 실패: {ex.Message}");
            }
        }

        // 상세 정보    
        private void listStudents_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = listStudents.SelectedIndex; // 선택한 인덱스번호가 저장돼요

            Modify modify = new Modify(); // 인스턴스 생성
            Student selectedStudent = modify.ModifyIndex(index, students); // 메서드값 넘겨받아요

            if (selectedStudent != null)
            {
                MessageBox.Show(selectedStudent.Introduce()); // 클래스의 메서드 호출
            }
            else
            {
                MessageBox.Show("오류");
            }
        }

        // 초기화
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearUI clearUI = new ClearUI();
            clearUI.ClearInputs(txtName, txtAge, txtMajor, listStudents);
            // 피드백: 이렇게 ui를 넘기는 경우는 잘 없음
        }
    }
}

'C#' 카테고리의 다른 글

이벤트 핸들러 학습 후 응용한 예제  (0) 2025.11.06
이벤트 핸들러 학습 예제  (0) 2025.11.06
"이벤트 핸들러"를 사용한 "인자 전달"  (0) 2025.11.05
인스턴스  (0) 2025.11.05
학생 추가 기능 분리  (0) 2025.11.05
'C#' 카테고리의 다른 글
  • 이벤트 핸들러 학습 후 응용한 예제
  • 이벤트 핸들러 학습 예제
  • "이벤트 핸들러"를 사용한 "인자 전달"
  • 인스턴스
joo_coding
joo_coding
2025.02.18~
  • joo_coding
    주코딩일지
    joo_coding
  • 전체
    오늘
    어제
    • 분류 전체보기 (219)
      • 일지 (19)
      • 계획표 (7)
      • 프로젝트 (6)
      • C언어 (35)
        • 연습장 (12)
      • C++ (3)
      • C# (34)
      • Python (28)
        • 연습장 (11)
      • TCP IP (4)
      • DB (2)
      • ubuntu (1)
      • Git (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    c언어 #vscode #gcc #윈도우 #c언어윈도우 #gcc윈도우 #vscode윈도우 #c #c++
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
joo_coding
학생조회프로그램 - 외부클래스 분리버전
상단으로

티스토리툴바