Код для Form1.cs
using System.Text.Json;
using System.Xml.Linq;
namespace AnyaTask6
{
public partial class Form1 : Form
{
private List<Note> notes = new();
private string filePath = "notes.txt";
private System.Windows.Forms.Timer checkTimer;
public Form1()
{
InitializeComponent();
LoadNotes();
UpdateList();
StartClock();
}
private void LoadNotes()
{
if (!File.Exists(filePath)) return;
string[] lines = File.ReadAllLines(filePath);
List<Note> loadedNotes = new List<Note>();
foreach (string line in lines)
{
string[] med = line.Split("-", StringSplitOptions.TrimEntries);
Note me = new Note();
me.Time = DateTime.Parse(med[0]);
me.Text = med[1];
loadedNotes.Add(me);
}
var now = DateTime.Now;
foreach (var note in loadedNotes)
{
if (note.Time > now)
notes.Add(note);
else if (note.Time <= now)
ShowAndRemovePastNote(note);
}
}
private void ShowAndRemovePastNote(Note note)
{
if (MessageBox.Show($"Задача просрочена {note.Time}:\n{note.Text}") == DialogResult.OK)
{
// Просто не добавляем в список
}
}
private void SaveNotes()
{
List<Note> sorted = notes.OrderBy(n => n.Time).ToList();
using (StreamWriter sw = new StreamWriter(filePath))
{
foreach (var note in sorted) {
sw.WriteLine(note);
}
}
MessageBox.Show("Список сохранён");
}
private void UpdateList()
{
listBoxNotes.Items.Clear();
foreach (var note in notes.OrderBy(n => n.Time))
listBoxNotes.Items.Add(note);
}
private void StartClock()
{
checkTimer = new System.Windows.Forms.Timer();
checkTimer.Interval = 5000;
checkTimer.Tick += (s, e) =>
{
lblTime.Text = DateTime.Now.ToString();
var expired = notes.Where(n => n.Time <= DateTime.Now).ToList();
foreach (var note in expired)
{
ShowAndRemovePastNote(note);
notes.Remove(note);
}
UpdateList();
};
checkTimer.Start();
}
private void btnAdd_Click(object sender, EventArgs e)
{
var time = dateTimePicker1.Value;
var text = txtText.Text.Trim();
if (string.IsNullOrWhiteSpace(text)) return;
if (time <= DateTime.Now)
{
ShowAndRemovePastNote(new Note { Time = time, Text = text });
return;
}
notes.Add(new Note { Time = time, Text = text });
UpdateList();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (listBoxNotes.SelectedItem is Note selected)
{
notes.Remove(selected);
UpdateList();
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (listBoxNotes.SelectedItem is Note selected)
{
var newTime = dateTimePicker1.Value;
selected.Text = txtText.Text;
selected.Time = newTime;
UpdateList();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveNotes();
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask6
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private Label lblTime;
private TextBox txtText;
private ListBox listBoxNotes;
private Button btnAdd;
private Button btnDelete;
private Button btnEdit;
private Button btnSave;
private Label lblText;
private Label lblDate;
private Label lblNotes;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
lblTime = new Label();
txtText = new TextBox();
listBoxNotes = new ListBox();
btnAdd = new Button();
btnDelete = new Button();
btnEdit = new Button();
btnSave = new Button();
lblText = new Label();
lblDate = new Label();
lblNotes = new Label();
dateTimePicker1 = new DateTimePicker();
SuspendLayout();
//
// lblTime
//
lblTime.AutoSize = true;
lblTime.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 204);
lblTime.Location = new Point(20, 15);
lblTime.Name = "lblTime";
lblTime.Size = new Size(115, 20);
lblTime.TabIndex = 0;
lblTime.Text = "Текущее время";
//
// txtText
//
txtText.Location = new Point(20, 70);
txtText.Multiline = true;
txtText.Name = "txtText";
txtText.Size = new Size(430, 60);
txtText.TabIndex = 2;
//
// listBoxNotes
//
listBoxNotes.FormattingEnabled = true;
listBoxNotes.Location = new Point(20, 210);
listBoxNotes.Name = "listBoxNotes";
listBoxNotes.Size = new Size(430, 124);
listBoxNotes.TabIndex = 7;
//
// btnAdd
//
btnAdd.Location = new Point(276, 160);
btnAdd.Name = "btnAdd";
btnAdd.Size = new Size(100, 30);
btnAdd.TabIndex = 5;
btnAdd.Text = "Добавить";
btnAdd.Click += btnAdd_Click;
//
// btnDelete
//
btnDelete.Location = new Point(382, 160);
btnDelete.Name = "btnDelete";
btnDelete.Size = new Size(100, 30);
btnDelete.TabIndex = 6;
btnDelete.Text = "Удалить";
btnDelete.Click += btnDelete_Click;
//
// btnEdit
//
btnEdit.Location = new Point(20, 360);
btnEdit.Name = "btnEdit";
btnEdit.Size = new Size(167, 30);
btnEdit.TabIndex = 9;
btnEdit.Text = "Редактировать";
btnEdit.Click += btnEdit_Click;
//
// btnSave
//
btnSave.Location = new Point(193, 360);
btnSave.Name = "btnSave";
btnSave.Size = new Size(100, 30);
btnSave.TabIndex = 10;
btnSave.Text = "Сохранить";
btnSave.Click += btnSave_Click;
//
// lblText
//
lblText.AutoSize = true;
lblText.Location = new Point(20, 50);
lblText.Name = "lblText";
lblText.Size = new Size(45, 20);
lblText.TabIndex = 1;
lblText.Text = "Текст";
//
// lblDate
//
lblDate.AutoSize = true;
lblDate.Location = new Point(20, 140);
lblDate.Name = "lblDate";
lblDate.Size = new Size(102, 20);
lblDate.TabIndex = 3;
lblDate.Text = "Дата и время";
//
// lblNotes
//
lblNotes.AutoSize = true;
lblNotes.Location = new Point(20, 190);
lblNotes.Name = "lblNotes";
lblNotes.Size = new Size(147, 20);
lblNotes.TabIndex = 8;
lblNotes.Text = "Список сообщений:";
//
// dateTimePicker1
//
dateTimePicker1.Location = new Point(20, 163);
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new Size(250, 27);
dateTimePicker1.TabIndex = 11;
//
// Form1
//
ClientSize = new Size(560, 400);
Controls.Add(dateTimePicker1);
Controls.Add(lblTime);
Controls.Add(lblText);
Controls.Add(txtText);
Controls.Add(lblDate);
Controls.Add(btnAdd);
Controls.Add(btnDelete);
Controls.Add(listBoxNotes);
Controls.Add(lblNotes);
Controls.Add(btnEdit);
Controls.Add(btnSave);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
Name = "Form1";
Text = "Записная книжка";
ResumeLayout(false);
PerformLayout();
}
private DateTimePicker dateTimePicker1;
}
}
Код для Note.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask6
{
public class Note
{
public DateTime Time { get; set; }
public string Text { get; set; }
public override string ToString()
{
return $"{Time} - {Text}";
}
}
}