Код для 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.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnyaTask8
{
public partial class Form1 : Form
{
public List<Patient> Patients = new List<Patient>();
public string filename = "patients.txt";
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
var pat = new Patient()
{
Fullname = textBox1.Text,
DataRozhdenia = dateTimePicker1.Value,
Address = textBox2.Text,
OMS = textBox3.Text,
};
Patients.Add(pat);
listBox1.Items.Clear();
foreach (var item in Patients)
{
listBox1.Items.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Patient patient)
{
var zab = new Zabolevanie()
{
Name = textBox4.Text,
DataObrash = dateTimePicker2.Value,
DataVipiski = dateTimePicker3.Value,
};
patient.Zabolevania.Add(zab);
listBox1.Items.Clear();
foreach (var item in Patients)
{
listBox1.Items.Add(item);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem is Patient pat)
{
Patients.Remove(pat);
listBox1.Items.Clear();
foreach (var item in Patients)
{
listBox1.Items.Add(item);
}
}
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
var poiskText = textBox5.Text.ToLower();
var naideniePatienti = new List<Patient>();
foreach (var item in Patients)
{
if (item.ToString().ToLower().Contains(poiskText))
{
naideniePatienti.Add(item);
}
}
listBox1.Items.Clear();
foreach (var item in naideniePatienti)
{
listBox1.Items.Add(item);
}
}
private void button4_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(filename))
{
writer.WriteLine(Patients.Count);
foreach (var p in Patients)
{
writer.WriteLine(p.Fullname);
writer.WriteLine(p.DataRozhdenia.ToString("yyyy-MM-dd"));
writer.WriteLine(p.Address);
writer.WriteLine(p.OMS);
writer.WriteLine(p.Zabolevania.Count);
foreach (var z in p.Zabolevania)
{
writer.WriteLine(z.Name);
writer.WriteLine(z.DataObrash.ToString("yyyy-MM-dd"));
writer.WriteLine(z.DataVipiski.ToString("yyyy-MM-dd"));
}
}
}
MessageBox.Show("Список пациентов сохранён");
}
private void Form1_Load(object sender, EventArgs e)
{
if (!File.Exists(filename))
return;
using (StreamReader reader = new StreamReader(filename))
{
if (!int.TryParse(reader.ReadLine(), out int count))
return;
for (int i = 0; i < count; i++)
{
string fullname = reader.ReadLine();
DateTime birthDate = DateTime.Parse(reader.ReadLine());
string address = reader.ReadLine();
string oms = reader.ReadLine();
Patient patient = new Patient
{
Fullname = fullname,
DataRozhdenia = birthDate,
Address = address,
OMS = oms,
Zabolevania = new List<Zabolevanie>()
};
if (int.TryParse(reader.ReadLine(), out int diseaseCount))
{
for (int j = 0; j < diseaseCount; j++)
{
string diseaseName = reader.ReadLine();
DateTime start = DateTime.Parse(reader.ReadLine());
DateTime end = DateTime.Parse(reader.ReadLine());
patient.Zabolevania.Add(new Zabolevanie
{
Name = diseaseName,
DataObrash = start,
DataVipiski = end
});
}
}
Patients.Add(patient);
}
}
listBox1.Items.Clear();
foreach (var p in Patients)
{
listBox1.Items.Add(p);
}
}
}
}
Код для Form1.Designer.cs
namespace AnyaTask8
{
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.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker3 = new System.Windows.Forms.DateTimePicker();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 16;
this.listBox1.Location = new System.Drawing.Point(22, 63);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(548, 228);
this.listBox1.TabIndex = 0;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(692, 28);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(200, 22);
this.textBox1.TabIndex = 1;
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(692, 66);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 22);
this.dateTimePicker1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(692, 94);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(200, 22);
this.textBox2.TabIndex = 3;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(692, 133);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(200, 22);
this.textBox3.TabIndex = 4;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(712, 241);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 22);
this.textBox4.TabIndex = 5;
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(612, 269);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(200, 22);
this.dateTimePicker2.TabIndex = 6;
//
// dateTimePicker3
//
this.dateTimePicker3.Location = new System.Drawing.Point(612, 298);
this.dateTimePicker3.Name = "dateTimePicker3";
this.dateTimePicker3.Size = new System.Drawing.Size(200, 22);
this.dateTimePicker3.TabIndex = 7;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(609, 241);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(99, 16);
this.label1.TabIndex = 8;
this.label1.Text = "Заболевание ";
//
// button1
//
this.button1.Location = new System.Drawing.Point(612, 336);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(200, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Добавить заболевание";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(612, 173);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(200, 23);
this.button2.TabIndex = 10;
this.button2.Text = "Добавить пациента";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(22, 297);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(105, 23);
this.button3.TabIndex = 11;
this.button3.Text = "Удалить";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(72, 28);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(245, 22);
this.textBox5.TabIndex = 12;
this.textBox5.TextChanged += new System.EventHandler(this.textBox5_TextChanged);
//
// button4
//
this.button4.Location = new System.Drawing.Point(134, 296);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(113, 23);
this.button4.TabIndex = 13;
this.button4.Text = "Сохранить";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(648, 28);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(38, 16);
this.label2.TabIndex = 14;
this.label2.Text = "ФИО";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(580, 71);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(106, 16);
this.label3.TabIndex = 15;
this.label3.Text = "Дата рождения";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(639, 100);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(47, 16);
this.label4.TabIndex = 16;
this.label4.Text = "Адрес";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(639, 139);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(37, 16);
this.label5.TabIndex = 17;
this.label5.Text = "ОМС";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(19, 31);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(47, 16);
this.label6.TabIndex = 18;
this.label6.Text = "Поиск";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(904, 450);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button4);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.dateTimePicker3);
this.Controls.Add(this.dateTimePicker2);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.DateTimePicker dateTimePicker2;
private System.Windows.Forms.DateTimePicker dateTimePicker3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
}
}
Код для Patient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask8
{
[Serializable]
public class Patient
{
public string Fullname { get; set; }
public DateTime DataRozhdenia { get; set; }
public string Address { get; set; }
public string OMS { get; set; }
public List<Zabolevanie> Zabolevania { get; set; } = new List<Zabolevanie>();
public override string ToString()
{
string zabolev = "";
foreach (var item in Zabolevania)
{
zabolev += item.ToString() + ",";
}
return $"{Fullname} ({DataRozhdenia}) - {OMS}: {zabolev}";
}
}
}
Код для Zabolevanie.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AnyaTask8
{
[Serializable]
public class Zabolevanie
{
public string Name { get; set; }
public DateTime DataObrash { get; set; }
public DateTime DataVipiski { get; set; }
public override string ToString()
{
return $"{Name}";
}
}
}