Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Runtime.InteropServices.ComTypes;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- namespace ToDoList
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- NameUpdate();
- }
- string filename = "Untitled";
- string todo_date, todo_task, todo_status;
- string current_date, current_task, current_status;
- string date_filter, todo_filter, filtered_task;
- private void Form1_Load(object sender, EventArgs e)
- {
- this.comboStatus.SelectedIndex = 3; // set default status เป็น Not Start
- this.comboFilter.SelectedIndex = 4; // set default filter เป็น All
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- todo_date = dateTimePicker1.Text;
- todo_task = txtTask.Text;
- todo_status = comboStatus.Text;
- if (todo_task == "") // check ถ้าช่อง task ไม่มีข้อความ ให้ขึ้นแจ้งเตือน
- {
- MessageBox.Show("Please enter your task.","To Do List",MessageBoxButtons.OK,MessageBoxIcon.Warning);
- }
- else
- {
- dgTask.Rows.Add(todo_date, todo_task, todo_status);
- FilterTask();
- }
- txtTask.Text = ""; // clear ค่าในช่อง task
- }
- int ComboMatch(string status_text)
- {
- switch (status_text)
- {
- case "Completed": return 0;
- case "In Progress": return 1;
- case "Cancelled": return 2;
- case "Not Start": return 3;
- }
- return 4;
- }
- private void dgTask_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- if (dgTask.CurrentRow != null)
- {
- current_date = dgTask.CurrentRow.Cells["Date"].Value.ToString();
- current_task = dgTask.CurrentRow.Cells["Task"].Value.ToString();
- current_status = dgTask.CurrentRow.Cells["Status"].Value.ToString();
- CultureInfo provider = CultureInfo.InvariantCulture; // set ให้ไม่สนใจ date format ของเครื่อง
- dateTimePicker1.Value = DateTime.ParseExact(current_date, "dd/MM/yyyy", provider);
- txtTask.Text = current_task;
- comboStatus.SelectedIndex = ComboMatch(current_status);
- }
- }
- private void btnEdit_Click(object sender, EventArgs e)
- {
- todo_date = dateTimePicker1.Text;
- todo_task = txtTask.Text;
- todo_status = comboStatus.Text;
- if (dgTask.CurrentRow != null)
- {
- dgTask.CurrentRow.Cells["Date"].Value = todo_date;
- dgTask.CurrentRow.Cells["Task"].Value = todo_task;
- dgTask.CurrentRow.Cells["Status"].Value = todo_status;
- }
- FilterTask();
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (dgTask.CurrentRow != null)
- {
- dgTask.Rows.RemoveAt(dgTask.CurrentRow.Index);
- FilterTask();
- }
- }
- void FilterTask()
- {
- date_filter = dateTimePicker2.Text;
- todo_filter = comboFilter.Text;
- filtered_task = "";
- foreach (DataGridViewRow row in dgTask.Rows)
- {
- if (todo_filter == "All")
- {
- if (row.Cells["Date"].Value.ToString() == date_filter)
- {
- filtered_task += row.Cells["Date"].Value.ToString() + " "
- + row.Cells["Task"].Value.ToString() + " "
- + row.Cells["Status"].Value.ToString() + "\n";
- }
- } else
- {
- if (row.Cells["Status"].Value.ToString() == todo_filter &&
- row.Cells["Date"].Value.ToString() == date_filter)
- {
- filtered_task += row.Cells["Date"].Value.ToString() + " "
- + row.Cells["Task"].Value.ToString() + " "
- + row.Cells["Status"].Value.ToString() + "\n";
- }
- }
- }
- lblTaskFilter.Text = filtered_task;
- }
- private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
- {
- FilterTask();
- }
- private void comboFilter_SelectedIndexChanged(object sender, EventArgs e)
- {
- FilterTask();
- }
- private void btnOpen_Click(object sender, EventArgs e)
- {
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- openFileDialog1.Filter = "Text Files|*.txt";
- openFileDialog1.Title = "Open Text File";
- openFileDialog1.ShowDialog();
- if (openFileDialog1.FileName != "")
- {
- filename = Path.GetFileName(openFileDialog1.FileName);
- dgTask.Rows.Clear();
- string[] lines = File.ReadAllLines(openFileDialog1.FileName);
- foreach (string line in lines)
- {
- string[] cols = line.Split('\t');
- dgTask.Rows.Add(cols[0], cols[1], cols[2]);
- }
- }
- NameUpdate();
- FilterTask();
- }
- private void btnSave_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "Text Files|*.txt";
- saveFileDialog1.Title = "Save as Text File";
- saveFileDialog1.ShowDialog();
- if (saveFileDialog1.FileName != "")
- {
- using (StreamWriter writer = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8))
- {
- foreach (DataGridViewRow row in dgTask.Rows)
- {
- string line = row.Cells["Date"].Value + "\t" + row.Cells["Task"].Value + "\t" + row.Cells["Status"].Value;
- writer.WriteLine(line);
- }
- }
- filename = Path.GetFileName(saveFileDialog1.FileName);
- }
- NameUpdate();
- }
- void NameUpdate()
- {
- this.Text = "To Do List - " + filename;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement