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;
namespace Student2
{
public partial class MainForm : Form
{
Timers timers = new Timers();
private Timer studentTimer;
DataTable table = new DataTable();
Create create = new Create();
public MainForm()
{
InitializeComponent();
timers.timerD += CreateStudent;
// column 추가
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Age", typeof(string));
table.Columns.Add("Major", typeof(string));
// 샘플 데이터 추가
table.Rows.Add("홍길동", "20", "컴퓨터공학");
table.Rows.Add("김철수", "22", "수학");
// 값들이 입력된 테이블을 DataGridView에 입력합니다.
MainGrid.DataSource = table;
}
public static string CurrentName; // static 변수로 선언해서 사용하는 방법도 있음
public static int CurrentAge;
public static string CurrentMajor;
// [ 조회 ]
private void btn_lookup_Click(object sender, EventArgs e)
{
DataGridView grid = new DataGridView();
int rowIndex = MainGrid.CurrentCell.RowIndex;
string name = MainGrid.Rows[rowIndex].Cells[0].Value.ToString(); // 이름
int age = Convert.ToInt32(MainGrid.Rows[rowIndex].Cells[1].Value); // 나이
string major = MainGrid.Rows[rowIndex].Cells[2].Value.ToString(); // 전공
CurrentName = name;
CurrentAge = age;
CurrentMajor = major;
DetailForm detailForm = new DetailForm();
//detailForm.refreshUi();
detailForm.ShowDialog();
}
// [ 타이머 설정 ] - Timers
private void OnStudentTimer()
{
studentTimer = new Timer();
studentTimer.Interval = 2000; // 2초 = 타이머의 간격
studentTimer.Tick += new EventHandler(timer1_Tick); // Tick = 간격마다 실행되는 이벤트
studentTimer.Start(); // 위에 설정한대로 타이머 시작
}
// [ UI 이벤트들 ] ////////////////////////////////////////////////////////////
private void btn_Start_Click(object sender, EventArgs e)
{
label1.Text = "시작";
OnStudentTimer();
}
private void btn_Stop_Click(object sender, EventArgs e)
{
label1.Text = "중지";
studentTimer.Stop();
MessageBox.Show("타이머 중지");
}
//////////////////////////////////////////////////////////////////////////////////
private void timer1_Tick(object sender, EventArgs e)
{
CreateStudent(); // Tick 마다 실행되는 메서드
}
private void CreateStudent()
{
// 객체를 생성하고 데이터를 추가하는 로직으로 변경 필요
//Student newStudent = new Student();
//newStudent.Name = "아무개";
//newStudent.Age = 25;
//newStudent.Major = "과학";
Student newStudent = create.StudentCreate();
table.Rows.Add(newStudent.Name, newStudent.Age.ToString(), newStudent.Major);
}
}
}
주코딩일지