Advertisement
Kelly-B9978

To Do List

Mar 2nd, 2024
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Runtime.InteropServices.ComTypes;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using System.Windows.Forms.VisualStyles;
  16.  
  17. namespace ToDoList
  18. {
  19.     public partial class Form1 : Form
  20.     {
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.             NameUpdate();
  25.         }
  26.  
  27.         string filename = "Untitled";
  28.         string todo_date, todo_task, todo_status;
  29.         string current_date, current_task, current_status;
  30.         string date_filter, todo_filter, filtered_task;
  31.  
  32.         private void Form1_Load(object sender, EventArgs e)
  33.         {
  34.             this.comboStatus.SelectedIndex = 3; // set default status เป็น Not Start
  35.             this.comboFilter.SelectedIndex = 4; // set default filter เป็น All
  36.         }
  37.  
  38.         private void btnAdd_Click(object sender, EventArgs e)
  39.         {
  40.             todo_date = dateTimePicker1.Text;
  41.             todo_task = txtTask.Text;
  42.             todo_status = comboStatus.Text;
  43.  
  44.             if (todo_task == "") // check ถ้าช่อง task ไม่มีข้อความ ให้ขึ้นแจ้งเตือน
  45.             {
  46.                 MessageBox.Show("Please enter your task.","To Do List",MessageBoxButtons.OK,MessageBoxIcon.Warning);
  47.             }
  48.             else
  49.             {
  50.                 dgTask.Rows.Add(todo_date, todo_task, todo_status);
  51.                 FilterTask();
  52.             }
  53.             txtTask.Text = ""; // clear ค่าในช่อง task
  54.         }
  55.  
  56.         int ComboMatch(string status_text)
  57.         {
  58.             switch (status_text)
  59.             {
  60.                 case "Completed": return 0;
  61.                 case "In Progress": return 1;
  62.                 case "Cancelled": return 2;
  63.                 case "Not Start": return 3;
  64.             }
  65.             return 4;
  66.         }
  67.  
  68.         private void dgTask_CellClick(object sender, DataGridViewCellEventArgs e)
  69.         {
  70.             if (dgTask.CurrentRow != null)
  71.             {
  72.                 current_date = dgTask.CurrentRow.Cells["Date"].Value.ToString();
  73.                 current_task = dgTask.CurrentRow.Cells["Task"].Value.ToString();
  74.                 current_status = dgTask.CurrentRow.Cells["Status"].Value.ToString();
  75.  
  76.                 CultureInfo provider = CultureInfo.InvariantCulture; // set ให้ไม่สนใจ date format ของเครื่อง
  77.                 dateTimePicker1.Value = DateTime.ParseExact(current_date, "dd/MM/yyyy", provider);
  78.                 txtTask.Text = current_task;
  79.                 comboStatus.SelectedIndex = ComboMatch(current_status);
  80.             }
  81.         }
  82.  
  83.         private void btnEdit_Click(object sender, EventArgs e)
  84.         {
  85.             todo_date = dateTimePicker1.Text;
  86.             todo_task = txtTask.Text;
  87.             todo_status = comboStatus.Text;
  88.  
  89.             if (dgTask.CurrentRow != null)
  90.             {
  91.                 dgTask.CurrentRow.Cells["Date"].Value = todo_date;
  92.                 dgTask.CurrentRow.Cells["Task"].Value = todo_task;
  93.                 dgTask.CurrentRow.Cells["Status"].Value = todo_status;
  94.             }
  95.             FilterTask();
  96.         }
  97.  
  98.         private void btnDelete_Click(object sender, EventArgs e)
  99.         {
  100.             if (dgTask.CurrentRow != null)
  101.             {
  102.                 dgTask.Rows.RemoveAt(dgTask.CurrentRow.Index);
  103.                 FilterTask();
  104.             }
  105.         }
  106.  
  107.         void FilterTask()
  108.         {
  109.             date_filter = dateTimePicker2.Text;
  110.             todo_filter = comboFilter.Text;
  111.             filtered_task = "";
  112.  
  113.             foreach (DataGridViewRow row in dgTask.Rows)
  114.             {
  115.                 if (todo_filter == "All")
  116.                 {
  117.                     if (row.Cells["Date"].Value.ToString() == date_filter)
  118.                     {
  119.                         filtered_task += row.Cells["Date"].Value.ToString() + "   "
  120.                             + row.Cells["Task"].Value.ToString() + "   "
  121.                             + row.Cells["Status"].Value.ToString() + "\n";
  122.                     }
  123.                 } else
  124.                 {
  125.                     if (row.Cells["Status"].Value.ToString() == todo_filter &&
  126.                         row.Cells["Date"].Value.ToString() == date_filter)
  127.                     {
  128.                         filtered_task += row.Cells["Date"].Value.ToString() + "   "
  129.                             + row.Cells["Task"].Value.ToString() + "   "
  130.                             + row.Cells["Status"].Value.ToString() + "\n";
  131.                     }
  132.                 }
  133.             }
  134.  
  135.             lblTaskFilter.Text = filtered_task;
  136.         }
  137.  
  138.         private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
  139.         {
  140.             FilterTask();
  141.         }
  142.  
  143.         private void comboFilter_SelectedIndexChanged(object sender, EventArgs e)
  144.         {
  145.             FilterTask();
  146.         }
  147.  
  148.         private void btnOpen_Click(object sender, EventArgs e)
  149.         {
  150.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  151.             openFileDialog1.Filter = "Text Files|*.txt";
  152.             openFileDialog1.Title = "Open Text File";
  153.             openFileDialog1.ShowDialog();
  154.  
  155.             if (openFileDialog1.FileName != "")
  156.             {
  157.                 filename = Path.GetFileName(openFileDialog1.FileName);
  158.                 dgTask.Rows.Clear();
  159.  
  160.                 string[] lines = File.ReadAllLines(openFileDialog1.FileName);
  161.                 foreach (string line in lines)
  162.                 {
  163.                     string[] cols = line.Split('\t');
  164.                     dgTask.Rows.Add(cols[0], cols[1], cols[2]);
  165.                 }
  166.             }
  167.  
  168.             NameUpdate();
  169.             FilterTask();
  170.         }
  171.  
  172.         private void btnSave_Click(object sender, EventArgs e)
  173.         {
  174.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  175.             saveFileDialog1.Filter = "Text Files|*.txt";
  176.             saveFileDialog1.Title = "Save as Text File";
  177.             saveFileDialog1.ShowDialog();
  178.  
  179.             if (saveFileDialog1.FileName != "")
  180.             {
  181.                 using (StreamWriter writer = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8))
  182.                 {
  183.                     foreach (DataGridViewRow row in dgTask.Rows)
  184.                     {
  185.                         string line = row.Cells["Date"].Value + "\t" + row.Cells["Task"].Value + "\t" + row.Cells["Status"].Value;
  186.                         writer.WriteLine(line);
  187.                     }
  188.                 }
  189.  
  190.                 filename = Path.GetFileName(saveFileDialog1.FileName);
  191.             }
  192.  
  193.             NameUpdate();
  194.         }
  195.  
  196.         void NameUpdate()
  197.         {
  198.             this.Text = "To Do List - " + filename;
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement