Thêm xóa sửa công việc ứng dụng Lập lịch C# Winform
Thêm xóa sửa công việc ứng dụng Lập lịch C# Winform
Lập trình ứng dụng Lập lịch với C# Winform

Danh sách bài học
Thêm xóa sửa công việc ứng dụng Lập lịch C# Winform
Thêm xóa sửa công việc ứng dụng Lập lịch C# Winform
Nội dung bài viết Học nhanh
Không có gì tuyệt vời hơn là luyện tập với ví dụ thực tế. Nào cùng nhau thử thách bản thân với ứng dụng: Lập lịch.
Bạn nên có kiến thức về:
- Lập trình Winform cơ bản
- Delegate – Event
- Timer
- NotifyIcon
- Registry
- Xử lý ngày tháng năm
Code Form1.Designer.cs
namespace Calender
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.panel6 = new System.Windows.Forms.Panel();
this.nmNotify = new System.Windows.Forms.NumericUpDown();
this.ckbNotify = new System.Windows.Forms.CheckBox();
this.btnToDay = new System.Windows.Forms.Button();
this.dtpkDate = new System.Windows.Forms.DateTimePicker();
this.panel2 = new System.Windows.Forms.Panel();
this.pnlMatrix = new System.Windows.Forms.Panel();
this.panel4 = new System.Windows.Forms.Panel();
this.btnNext = new System.Windows.Forms.Button();
this.btnPreviours = new System.Windows.Forms.Button();
this.btnSunday = new System.Windows.Forms.Button();
this.btnSaturday = new System.Windows.Forms.Button();
this.btnFriday = new System.Windows.Forms.Button();
this.btnThusday = new System.Windows.Forms.Button();
this.btnWednesday = new System.Windows.Forms.Button();
this.btnTuesday = new System.Windows.Forms.Button();
this.btnMonday = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.panel3.SuspendLayout();
this.panel6.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nmNotify)).BeginInit();
this.panel2.SuspendLayout();
this.panel4.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.panel2);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(741, 356);
this.panel1.TabIndex = 0;
//
// panel3
//
this.panel3.Controls.Add(this.panel6);
this.panel3.Controls.Add(this.btnToDay);
this.panel3.Controls.Add(this.dtpkDate);
this.panel3.Location = new System.Drawing.Point(3, 3);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(735, 28);
this.panel3.TabIndex = 1;
//
// panel6
//
this.panel6.Controls.Add(this.nmNotify);
this.panel6.Controls.Add(this.ckbNotify);
this.panel6.Location = new System.Drawing.Point(7, 0);
this.panel6.Name = "panel6";
this.panel6.Size = new System.Drawing.Size(210, 28);
this.panel6.TabIndex = 2;
//
// nmNotify
//
this.nmNotify.Location = new System.Drawing.Point(81, 3);
this.nmNotify.Maximum = new decimal(new int[] {
3600,
0,
0,
0});
this.nmNotify.Minimum = new decimal(new int[] {
1,
0,
0,
0});
this.nmNotify.Name = "nmNotify";
this.nmNotify.Size = new System.Drawing.Size(60, 20);
this.nmNotify.TabIndex = 1;
this.nmNotify.Value = new decimal(new int[] {
1,
0,
0,
0});
//
// ckbNotify
//
this.ckbNotify.AutoSize = true;
this.ckbNotify.Location = new System.Drawing.Point(3, 4);
this.ckbNotify.Name = "ckbNotify";
this.ckbNotify.Size = new System.Drawing.Size(78, 17);
this.ckbNotify.TabIndex = 0;
this.ckbNotify.Text = "Thông báo";
this.ckbNotify.UseVisualStyleBackColor = true;
//
// btnToDay
//
this.btnToDay.Location = new System.Drawing.Point(493, 0);
this.btnToDay.Name = "btnToDay";
this.btnToDay.Size = new System.Drawing.Size(75, 23);
this.btnToDay.TabIndex = 1;
this.btnToDay.Text = "Hôm nay";
this.btnToDay.UseVisualStyleBackColor = true;
this.btnToDay.Click += new System.EventHandler(this.btnToDay_Click);
//
// dtpkDate
//
this.dtpkDate.Location = new System.Drawing.Point(287, 3);
this.dtpkDate.Name = "dtpkDate";
this.dtpkDate.Size = new System.Drawing.Size(200, 20);
this.dtpkDate.TabIndex = 0;
this.dtpkDate.ValueChanged += new System.EventHandler(this.dtpkDate_ValueChanged);
//
// panel2
//
this.panel2.Controls.Add(this.pnlMatrix);
this.panel2.Controls.Add(this.panel4);
this.panel2.Location = new System.Drawing.Point(3, 37);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(735, 314);
this.panel2.TabIndex = 0;
//
// pnlMatrix
//
this.pnlMatrix.Location = new System.Drawing.Point(88, 55);
this.pnlMatrix.Name = "pnlMatrix";
this.pnlMatrix.Size = new System.Drawing.Size(561, 254);
this.pnlMatrix.TabIndex = 1;
//
// panel4
//
this.panel4.Controls.Add(this.btnNext);
this.panel4.Controls.Add(this.btnPreviours);
this.panel4.Controls.Add(this.btnSunday);
this.panel4.Controls.Add(this.btnSaturday);
this.panel4.Controls.Add(this.btnFriday);
this.panel4.Controls.Add(this.btnThusday);
this.panel4.Controls.Add(this.btnWednesday);
this.panel4.Controls.Add(this.btnTuesday);
this.panel4.Controls.Add(this.btnMonday);
this.panel4.Location = new System.Drawing.Point(3, 3);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(729, 46);
this.panel4.TabIndex = 0;
//
// btnNext
//
this.btnNext.Location = new System.Drawing.Point(651, 3);
this.btnNext.Name = "btnNext";
this.btnNext.Size = new System.Drawing.Size(75, 40);
this.btnNext.TabIndex = 8;
this.btnNext.Text = "Tháng sau";
this.btnNext.UseVisualStyleBackColor = true;
this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
//
// btnPreviours
//
this.btnPreviours.Location = new System.Drawing.Point(4, 3);
this.btnPreviours.Name = "btnPreviours";
this.btnPreviours.Size = new System.Drawing.Size(75, 40);
this.btnPreviours.TabIndex = 7;
this.btnPreviours.Text = "Tháng trước";
this.btnPreviours.UseVisualStyleBackColor = true;
this.btnPreviours.Click += new System.EventHandler(this.btnPreviours_Click);
//
// btnSunday
//
this.btnSunday.Location = new System.Drawing.Point(571, 3);
this.btnSunday.Name = "btnSunday";
this.btnSunday.Size = new System.Drawing.Size(75, 40);
this.btnSunday.TabIndex = 6;
this.btnSunday.Text = "Chủ nhật";
this.btnSunday.UseVisualStyleBackColor = true;
//
// btnSaturday
//
this.btnSaturday.Location = new System.Drawing.Point(490, 3);
this.btnSaturday.Name = "btnSaturday";
this.btnSaturday.Size = new System.Drawing.Size(75, 40);
this.btnSaturday.TabIndex = 5;
this.btnSaturday.Text = "Thứ 7";
this.btnSaturday.UseVisualStyleBackColor = true;
//
// btnFriday
//
this.btnFriday.Location = new System.Drawing.Point(409, 3);
this.btnFriday.Name = "btnFriday";
this.btnFriday.Size = new System.Drawing.Size(75, 40);
this.btnFriday.TabIndex = 4;
this.btnFriday.Text = "Thứ 6";
this.btnFriday.UseVisualStyleBackColor = true;
//
// btnThusday
//
this.btnThusday.Location = new System.Drawing.Point(328, 3);
this.btnThusday.Name = "btnThusday";
this.btnThusday.Size = new System.Drawing.Size(75, 40);
this.btnThusday.TabIndex = 3;
this.btnThusday.Text = "Thứ 5";
this.btnThusday.UseVisualStyleBackColor = true;
//
// btnWednesday
//
this.btnWednesday.Location = new System.Drawing.Point(247, 3);
this.btnWednesday.Name = "btnWednesday";
this.btnWednesday.Size = new System.Drawing.Size(75, 40);
this.btnWednesday.TabIndex = 2;
this.btnWednesday.Text = "Thứ 4";
this.btnWednesday.UseVisualStyleBackColor = true;
//
// btnTuesday
//
this.btnTuesday.Location = new System.Drawing.Point(166, 3);
this.btnTuesday.Name = "btnTuesday";
this.btnTuesday.Size = new System.Drawing.Size(75, 40);
this.btnTuesday.TabIndex = 1;
this.btnTuesday.Text = "Thứ 3";
this.btnTuesday.UseVisualStyleBackColor = true;
//
// btnMonday
//
this.btnMonday.Location = new System.Drawing.Point(85, 3);
this.btnMonday.Name = "btnMonday";
this.btnMonday.Size = new System.Drawing.Size(75, 40);
this.btnMonday.TabIndex = 0;
this.btnMonday.Text = "Thứ 2";
this.btnMonday.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(765, 370);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Lập lịch công việc";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.panel1.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.panel6.ResumeLayout(false);
this.panel6.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nmNotify)).EndInit();
this.panel2.ResumeLayout(false);
this.panel4.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Panel panel6;
private System.Windows.Forms.NumericUpDown nmNotify;
private System.Windows.Forms.CheckBox ckbNotify;
private System.Windows.Forms.Button btnToDay;
private System.Windows.Forms.DateTimePicker dtpkDate;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Panel pnlMatrix;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Button btnNext;
private System.Windows.Forms.Button btnPreviours;
private System.Windows.Forms.Button btnSunday;
private System.Windows.Forms.Button btnSaturday;
private System.Windows.Forms.Button btnFriday;
private System.Windows.Forms.Button btnThusday;
private System.Windows.Forms.Button btnWednesday;
private System.Windows.Forms.Button btnTuesday;
private System.Windows.Forms.Button btnMonday;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
Code Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace Calender
{
public partial class Form1 : Form
{
#region Peoperties
private string filePath = "data.xml";
private List<List<Button>> matrix;
public List<List<Button>> Matrix
{
get { return matrix; }
set { matrix = value; }
}
private PlanData job;
public PlanData Job
{
get { return job; }
set { job = value; }
}
private List<string> dateOfWeek = new List<string>(){ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
#endregion
public Form1()
{
InitializeComponent();
LoadMatrix();
try
{
Job = DeserializeFromXML(filePath) as PlanData;
}
catch
{
SetDefaultJob();
}
}
void SetDefaultJob()
{
Job = new PlanData();
Job.Job = new List<PlanItem>();
Job.Job.Add(new PlanItem()
{
Date = DateTime.Now,
FromTime = new Point(4, 0),
ToTime = new Point(5, 0),
Job = "Thử nghiệm thôi",
Status = PlanItem.ListStatus[(int)EPlanItem.COMING]
});
}
void LoadMatrix()
{
Matrix = new List<List<Button>>();
Button oldBtn = new Button() { Width = 0, Height = 0, Location = new Point(-Cons.margin, 0) };
for (int i = 0; i < Cons.DayOfColumn; i++)
{
Matrix.Add(new List<Button>());
for (int j = 0; j < Cons.DayOfWeek; j++)
{
Button btn = new Button() { Width = Cons.dateButtonWidth, Height = Cons.dateButtonHeight};
btn.Location = new Point(oldBtn.Location.X + oldBtn.Width + Cons.margin, oldBtn.Location.Y);
btn.Click += btn_Click;
pnlMatrix.Controls.Add(btn);
Matrix[i].Add(btn);
oldBtn = btn;
}
oldBtn = new Button() { Width = 0, Height = 0, Location = new Point(-Cons.margin, oldBtn.Location.Y + Cons.dateButtonHeight) };
}
SetDefaultDate();
}
void btn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty((sender as Button).Text))
return;
DailyPlan daily = new DailyPlan(new DateTime(dtpkDate.Value.Year, dtpkDate.Value.Month, Convert.ToInt32((sender as Button).Text)), Job);
daily.ShowDialog();
}
int DayOfMonth(DateTime date)
{
switch(date.Month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if ((date.Year % 4 == 0 && date.Year % 100 != 0) || date.Year % 400 == 0)
return 29;
else
return 28;
default:
return 30;;
}
}
void AddNumberIntoMatrixByDate(DateTime date)
{
ClearMatrix();
DateTime useDate = new DateTime(date.Year, date.Month, 1);
int line = 0;
for (int i = 1; i <= DayOfMonth(date); i++)
{
int column = dateOfWeek.IndexOf(useDate.DayOfWeek.ToString());
Button btn = Matrix[line][column];
btn.Text = i.ToString();
if (isEqualDate(useDate, DateTime.Now))
{
btn.BackColor = Color.Yellow;
}
if (isEqualDate(useDate, date))
{
btn.BackColor = Color.Aqua;
}
if (column >= 6)
line++;
useDate = useDate.AddDays(1);
}
}
bool isEqualDate(DateTime dateA, DateTime dateB)
{
return dateA.Year == dateB.Year && dateA.Month == dateB.Month && dateA.Day == dateB.Day;
}
void ClearMatrix()
{
for (int i = 0; i < Matrix.Count; i++)
{
for (int j = 0; j < Matrix[i].Count; j++)
{
Button btn = Matrix[i][j];
btn.Text = "";
btn.BackColor = Color.WhiteSmoke;
}
}
}
void SetDefaultDate()
{
dtpkDate.Value = DateTime.Now;
}
private void dtpkDate_ValueChanged(object sender, EventArgs e)
{
AddNumberIntoMatrixByDate((sender as DateTimePicker).Value);
}
private void btnNext_Click(object sender, EventArgs e)
{
dtpkDate.Value = dtpkDate.Value.AddMonths(1);
}
private void btnPreviours_Click(object sender, EventArgs e)
{
dtpkDate.Value = dtpkDate.Value.AddMonths(-1);
}
private void btnToDay_Click(object sender, EventArgs e)
{
SetDefaultDate();
}
private void SerializeToXML(object data, string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
XmlSerializer sr = new XmlSerializer(typeof(PlanData));
sr.Serialize(fs, data);
fs.Close();
}
private object DeserializeFromXML(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
try
{
XmlSerializer sr = new XmlSerializer(typeof(PlanData));
object result = sr.Deserialize(fs);
fs.Close();
return result;
}
catch (Exception e)
{
fs.Close();
throw new NotImplementedException();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
SerializeToXML(Job, filePath);
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
Code DailyPlan.Designer.cs
namespace Calender
{
partial class DailyPlan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.panel3 = new System.Windows.Forms.Panel();
this.btnPrevioursDay = new System.Windows.Forms.Button();
this.btnNextDay = new System.Windows.Forms.Button();
this.dtpkDate = new System.Windows.Forms.DateTimePicker();
this.pnlJob = new System.Windows.Forms.Panel();
this.mnsMain = new System.Windows.Forms.MenuStrip();
this.mnsiAddJob = new System.Windows.Forms.ToolStripMenuItem();
this.mnsiToDay = new System.Windows.Forms.ToolStripMenuItem();
this.panel1.SuspendLayout();
this.panel3.SuspendLayout();
this.mnsMain.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.panel3);
this.panel1.Controls.Add(this.pnlJob);
this.panel1.Location = new System.Drawing.Point(12, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(770, 484);
this.panel1.TabIndex = 0;
//
// panel3
//
this.panel3.Controls.Add(this.btnPrevioursDay);
this.panel3.Controls.Add(this.btnNextDay);
this.panel3.Controls.Add(this.dtpkDate);
this.panel3.Location = new System.Drawing.Point(3, 3);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(764, 27);
this.panel3.TabIndex = 1;
//
// btnPrevioursDay
//
this.btnPrevioursDay.Location = new System.Drawing.Point(3, 1);
this.btnPrevioursDay.Name = "btnPrevioursDay";
this.btnPrevioursDay.Size = new System.Drawing.Size(75, 23);
this.btnPrevioursDay.TabIndex = 3;
this.btnPrevioursDay.Text = "Hôm qua";
this.btnPrevioursDay.UseVisualStyleBackColor = true;
this.btnPrevioursDay.Click += new System.EventHandler(this.btnPrevioursDay_Click);
//
// btnNextDay
//
this.btnNextDay.Location = new System.Drawing.Point(686, 1);
this.btnNextDay.Name = "btnNextDay";
this.btnNextDay.Size = new System.Drawing.Size(75, 23);
this.btnNextDay.TabIndex = 2;
this.btnNextDay.Text = "Ngày mai";
this.btnNextDay.UseVisualStyleBackColor = true;
this.btnNextDay.Click += new System.EventHandler(this.btnNextDay_Click);
//
// dtpkDate
//
this.dtpkDate.Location = new System.Drawing.Point(279, 3);
this.dtpkDate.Name = "dtpkDate";
this.dtpkDate.Size = new System.Drawing.Size(200, 20);
this.dtpkDate.TabIndex = 1;
this.dtpkDate.ValueChanged += new System.EventHandler(this.dtpkDate_ValueChanged);
//
// pnlJob
//
this.pnlJob.Location = new System.Drawing.Point(3, 32);
this.pnlJob.Name = "pnlJob";
this.pnlJob.Size = new System.Drawing.Size(764, 447);
this.pnlJob.TabIndex = 0;
//
// mnsMain
//
this.mnsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.mnsiAddJob,
this.mnsiToDay});
this.mnsMain.Location = new System.Drawing.Point(0, 0);
this.mnsMain.Name = "mnsMain";
this.mnsMain.Size = new System.Drawing.Size(782, 24);
this.mnsMain.TabIndex = 1;
this.mnsMain.Text = "menuStrip1";
//
// mnsiAddJob
//
this.mnsiAddJob.Name = "mnsiAddJob";
this.mnsiAddJob.Size = new System.Drawing.Size(74, 20);
this.mnsiAddJob.Text = "Thêm việc";
this.mnsiAddJob.Click += new System.EventHandler(this.mnsiAddJob_Click);
//
// mnsiToDay
//
this.mnsiToDay.Name = "mnsiToDay";
this.mnsiToDay.Size = new System.Drawing.Size(68, 20);
this.mnsiToDay.Text = "Hôm nay";
this.mnsiToDay.Click += new System.EventHandler(this.mnsiToDay_Click);
//
// DailyPlan
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(782, 511);
this.Controls.Add(this.panel1);
this.Controls.Add(this.mnsMain);
this.MainMenuStrip = this.mnsMain;
this.Name = "DailyPlan";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Lịch trong ngày";
this.panel1.ResumeLayout(false);
this.panel3.ResumeLayout(false);
this.mnsMain.ResumeLayout(false);
this.mnsMain.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Button btnPrevioursDay;
private System.Windows.Forms.Button btnNextDay;
private System.Windows.Forms.DateTimePicker dtpkDate;
private System.Windows.Forms.Panel pnlJob;
private System.Windows.Forms.MenuStrip mnsMain;
private System.Windows.Forms.ToolStripMenuItem mnsiAddJob;
private System.Windows.Forms.ToolStripMenuItem mnsiToDay;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
Code DailyPlan.cs
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 Calender
{
public partial class DailyPlan : Form
{
private DateTime date;
public DateTime Date
{
get { return date; }
set { date = value; }
}
private PlanData job;
public PlanData Job
{
get { return job; }
set { job = value; }
}
FlowLayoutPanel fPanel = new FlowLayoutPanel();
public DailyPlan(DateTime date, PlanData job)
{
InitializeComponent();
this.Date = date;
this.Job = job;
fPanel.Width = pnlJob.Width;
fPanel.Height = pnlJob.Height;
pnlJob.Controls.Add(fPanel);
dtpkDate.Value = Date;
}
void ShowJobByDate(DateTime date)
{
fPanel.Controls.Clear();
if (Job != null && Job.Job != null)
{
List<PlanItem> todayJob = GetJobByDay(date);
for (int i = 0; i < todayJob.Count; i++)
{
AddJob(todayJob[i]);
}
}
}
void AddJob(PlanItem job)
{
AJob aJob = new AJob(job);
aJob.Edited += aJob_Edited;
aJob.Deleted += aJob_Deleteed;
fPanel.Controls.Add(aJob);
}
void aJob_Deleteed(object sender, EventArgs e)
{
AJob uc = sender as AJob;
PlanItem job = uc.Job;
fPanel.Controls.Remove(uc);
Job.Job.Remove(job);
}
void aJob_Edited(object sender, EventArgs e)
{
}
List<PlanItem> GetJobByDay(DateTime date)
{
return Job.Job.Where(p=>p.Date.Year == date.Year && p.Date.Month == date.Month && p.Date.Day == date.Day).ToList();
}
private void dtpkDate_ValueChanged(object sender, EventArgs e)
{
ShowJobByDate((sender as DateTimePicker).Value);
}
private void btnNextDay_Click(object sender, EventArgs e)
{
dtpkDate.Value = dtpkDate.Value.AddDays(1);
}
private void btnPrevioursDay_Click(object sender, EventArgs e)
{
dtpkDate.Value = dtpkDate.Value.AddDays(-1);
}
private void mnsiAddJob_Click(object sender, EventArgs e)
{
PlanItem item = new PlanItem() { Date = dtpkDate.Value};
Job.Job.Add(item);
AddJob(item);
}
private void mnsiToDay_Click(object sender, EventArgs e)
{
dtpkDate.Value = DateTime.Now;
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
Code AJob.Designer.cs
namespace Calender
{
partial class AJob
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.btnDelete = new System.Windows.Forms.Button();
this.btnEdit = new System.Windows.Forms.Button();
this.cbStatus = new System.Windows.Forms.ComboBox();
this.panel2 = new System.Windows.Forms.Panel();
this.nmToMinute = new System.Windows.Forms.NumericUpDown();
this.nmToHours = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
this.nmFromMinute = new System.Windows.Forms.NumericUpDown();
this.nmFromHours = new System.Windows.Forms.NumericUpDown();
this.txbJob = new System.Windows.Forms.TextBox();
this.ckbDone = new System.Windows.Forms.CheckBox();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nmToMinute)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nmToHours)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nmFromMinute)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nmFromHours)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.btnDelete);
this.panel1.Controls.Add(this.btnEdit);
this.panel1.Controls.Add(this.cbStatus);
this.panel1.Controls.Add(this.panel2);
this.panel1.Controls.Add(this.txbJob);
this.panel1.Controls.Add(this.ckbDone);
this.panel1.Location = new System.Drawing.Point(3, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(747, 30);
this.panel1.TabIndex = 0;
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(699, 0);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(45, 23);
this.btnDelete.TabIndex = 5;
this.btnDelete.Text = "Xóa";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// btnEdit
//
this.btnEdit.Location = new System.Drawing.Point(650, 0);
this.btnEdit.Name = "btnEdit";
this.btnEdit.Size = new System.Drawing.Size(45, 23);
this.btnEdit.TabIndex = 4;
this.btnEdit.Text = "Sửa";
this.btnEdit.UseVisualStyleBackColor = true;
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
//
// cbStatus
//
this.cbStatus.FormattingEnabled = true;
this.cbStatus.Location = new System.Drawing.Point(541, 2);
this.cbStatus.Name = "cbStatus";
this.cbStatus.Size = new System.Drawing.Size(103, 21);
this.cbStatus.TabIndex = 3;
//
// panel2
//
this.panel2.Controls.Add(this.nmToMinute);
this.panel2.Controls.Add(this.nmToHours);
this.panel2.Controls.Add(this.label1);
this.panel2.Controls.Add(this.nmFromMinute);
this.panel2.Controls.Add(this.nmFromHours);
this.panel2.Location = new System.Drawing.Point(338, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(200, 30);
this.panel2.TabIndex = 2;
//
// nmToMinute
//
this.nmToMinute.Location = new System.Drawing.Point(160, 3);
this.nmToMinute.Name = "nmToMinute";
this.nmToMinute.Size = new System.Drawing.Size(37, 20);
this.nmToMinute.TabIndex = 4;
//
// nmToHours
//
this.nmToHours.Location = new System.Drawing.Point(117, 3);
this.nmToHours.Name = "nmToHours";
this.nmToHours.Size = new System.Drawing.Size(37, 20);
this.nmToHours.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(89, 5);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(27, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Đến";
//
// nmFromMinute
//
this.nmFromMinute.Location = new System.Drawing.Point(46, 3);
this.nmFromMinute.Name = "nmFromMinute";
this.nmFromMinute.Size = new System.Drawing.Size(37, 20);
this.nmFromMinute.TabIndex = 1;
//
// nmFromHours
//
this.nmFromHours.Location = new System.Drawing.Point(3, 3);
this.nmFromHours.Name = "nmFromHours";
this.nmFromHours.Size = new System.Drawing.Size(37, 20);
this.nmFromHours.TabIndex = 0;
//
// txbJob
//
this.txbJob.Location = new System.Drawing.Point(24, 0);
this.txbJob.Name = "txbJob";
this.txbJob.Size = new System.Drawing.Size(308, 20);
this.txbJob.TabIndex = 1;
//
// ckbDone
//
this.ckbDone.AutoSize = true;
this.ckbDone.Location = new System.Drawing.Point(3, 3);
this.ckbDone.Name = "ckbDone";
this.ckbDone.Size = new System.Drawing.Size(15, 14);
this.ckbDone.TabIndex = 0;
this.ckbDone.UseVisualStyleBackColor = true;
this.ckbDone.CheckedChanged += new System.EventHandler(this.ckbDone_CheckedChanged);
//
// AJob
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel1);
this.Name = "AJob";
this.Size = new System.Drawing.Size(753, 33);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nmToMinute)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nmToHours)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nmFromMinute)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nmFromHours)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.NumericUpDown nmFromHours;
private System.Windows.Forms.TextBox txbJob;
private System.Windows.Forms.CheckBox ckbDone;
private System.Windows.Forms.Button btnDelete;
private System.Windows.Forms.Button btnEdit;
private System.Windows.Forms.ComboBox cbStatus;
private System.Windows.Forms.NumericUpDown nmToMinute;
private System.Windows.Forms.NumericUpDown nmToHours;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.NumericUpDown nmFromMinute;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
Code AJob.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calender
{
public partial class AJob : UserControl
{
private PlanItem job;
public PlanItem Job
{
get { return job; }
set { job = value; }
}
private event EventHandler edited;
public event EventHandler Edited
{
add { edited += value; }
remove { edited -= value; }
}
private event EventHandler deleted;
public event EventHandler Deleted
{
add { deleted += value; }
remove { deleted -= value; }
}
public AJob(PlanItem job)
{
InitializeComponent();
cbStatus.DataSource = PlanItem.ListStatus;
this.Job = job;
ShowInfo();
}
void ShowInfo()
{
txbJob.Text = Job.Job;
nmFromHours.Value = Job.FromTime.X;
nmFromMinute.Value = Job.FromTime.Y;
nmToHours.Value = Job.ToTime.X;
nmToMinute.Value = Job.ToTime.Y;
cbStatus.SelectedIndex = PlanItem.ListStatus.IndexOf(Job.Status);
ckbDone.Checked = PlanItem.ListStatus.IndexOf(Job.Status) == (int)EPlanItem.DONE ? true : false;
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (deleted != null)
deleted(this, new EventArgs());
}
private void btnEdit_Click(object sender, EventArgs e)
{
Job.Job = txbJob.Text;
Job.FromTime = new Point((int)nmFromHours.Value, (int)nmFromMinute.Value);
Job.ToTime = new Point((int)nmToHours.Value, (int)nmToMinute.Value);
Job.Status = PlanItem.ListStatus[cbStatus.SelectedIndex];
if (edited != null)
edited(this, new EventArgs());
}
private void ckbDone_CheckedChanged(object sender, EventArgs e)
{
cbStatus.SelectedIndex = ckbDone.Checked ? (int)EPlanItem.DONE : (int)EPlanItem.DOING;
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
Code Cons.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calender
{
class Cons
{
public static int DayOfWeek = 7;
public static int DayOfColumn = 6;
public static int dateButtonWidth = 75;
public static int dateButtonHeight = 40;
public static int margin = 6;
}
}
1234567891011121314151617181920
Code PlanItem.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calender
{
public class PlanItem
{
private DateTime date;
public DateTime Date
{
get { return date; }
set { date = value; }
}
private string job;
public string Job
{
get { return job; }
set { job = value; }
}
private Point fromTime;
public Point FromTime
{
get { return fromTime; }
set { fromTime = value; }
}
private Point toTime;
public Point ToTime
{
get { return toTime; }
set { toTime = value; }
}
private string status;
public string Status
{
get { return status; }
set { status = value; }
}
public static List<string> ListStatus = new List<string>() { "DONE", "DOING", "COMING", "MISSED"};
}
public enum EPlanItem
{
DONE,
DOING,
COMING,
MISSED
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
Code PlanData.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calender
{
[Serializable]
public class PlanData
{
private List<PlanItem> job;
public List<PlanItem> Job
{
get { return job; }
set { job = value; }
}
}
}
123456789101112131415161718192021
File ứng dụng Demo
Bài sau chúng ta sẽ cùng nhau tạo thông báo.
Đừng quên: “Luyện tập – Thử thách – Không ngại khó”
Tải xuống
Tài liệu
Nhằm phục vụ mục đích học tập Offline của cộng đồng, Kteam hỗ trợ tính năng lưu trữ nội dung bài học Thêm xóa sửa công việc ứng dụng Lập lịch C# Winform dưới dạng file PDF trong link bên dưới.
Ngoài ra, bạn cũng có thể tìm thấy các tài liệu được đóng góp từ cộng đồng ở mục TÀI LIỆU trên thư viện Howkteam.com
Đừng quên like và share để ủng hộ Kteam và tác giả nhé!

Thảo luận
Nếu bạn có bất kỳ khó khăn hay thắc mắc gì về khóa học, đừng ngần ngại đặt câu hỏi trong phần bên dưới hoặc trong mục HỎI & ĐÁP trên thư viện Howkteam.com để nhận được sự hỗ trợ từ cộng đồng.
Tác giả/Dịch giả
Khóa học
Lập trình ứng dụng Lập lịch với C# Winform
Serial tutorial lập trình ứng dụng Lập lịch với C# Winform
a ơi e xóa các control rồi xong khi chạy lại ứng dụng thì vẫn hiện là bị sao ạ?
Anh ơi!
Khi em làm đến cái event addjob.click thì ngay chỗ Job.Job.Add(item); nó lại báo lỗi là:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Anh có thể chị em cách sửa không ạ!
Cảm ơn anh!!!