0

Код для Form1.cs


using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;

namespace Task2
{
    public partial class Form1 : Form
    {
        List<Patient> patients = new List<Patient>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("Patient.dat"))
            {
                using (BinaryReader reader = new BinaryReader(File.Open("Patient.dat", FileMode.Open)))
                {
                    int count = reader.ReadInt32();
                    for (int i = 0; i < count; i++)
                    {
                        Patient p = new Patient
                        {
                            SuName = reader.ReadString(),
                            Name = reader.ReadString(),
                            FathersName = reader.ReadString(),
                            DataBirth = DateTime.FromBinary(reader.ReadInt64()),
                            Adress = reader.ReadString(),
                            Oms = reader.ReadString(),
                            DataPostuplenia = DateTime.FromBinary(reader.ReadInt64()),
                            Diagnoz = reader.ReadString()
                        };

                        patients.Add(p);
                        listBox1.Items.Add($"{p.SuName} {p.Name} {p.FathersName}");
                    }
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Patient patient = new Patient();
            patient.SuName = textBox1.Text;
            patient.Name = textBox2.Text;
            patient.FathersName = textBox3.Text;
            patient.DataBirth = dateTimePicker1.Value.Date;
            patient.Adress = textBox4.Text;
            patient.Oms = textBox5.Text;
            patient.DataPostuplenia = dateTimePicker2.Value;
            patient.Diagnoz = textBox6.Text;
            patients.Add(patient);
            listBox1.Items.Add(patient.SuName + " " + patient.Name + " " + patient.FathersName);

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Patient patient = patients[listBox1.SelectedIndex];
            listBox2.Items.Clear();
            listBox2.Items.Add(patient.DataBirth);
            listBox2.Items.Add(patient.Adress);
            listBox2.Items.Add(patient.Oms);
            listBox2.Items.Add(patient.DataPostuplenia);
            listBox2.Items.Add(patient.Diagnoz);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (BinaryWriter writer = new BinaryWriter(File.Open("Patient.dat", FileMode.Create)))
            {
                writer.Write(patients.Count);
                foreach (var p in patients)
                {
                    writer.Write(p.SuName);
                    writer.Write(p.Name);
                    writer.Write(p.FathersName);
                    writer.Write(p.DataBirth.ToBinary());
                    writer.Write(p.Adress);
                    writer.Write(p.Oms);
                    writer.Write(p.DataPostuplenia.ToBinary());
                    writer.Write(p.Diagnoz);
                }
            }
        }
    }
}

Код для Form1.Designer.cs


namespace Task2
{
    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()
        {
            textBox1 = new TextBox();
            textBox2 = new TextBox();
            textBox3 = new TextBox();
            dateTimePicker1 = new DateTimePicker();
            label1 = new Label();
            label2 = new Label();
            label3 = new Label();
            label4 = new Label();
            textBox4 = new TextBox();
            textBox5 = new TextBox();
            dateTimePicker2 = new DateTimePicker();
            textBox6 = new TextBox();
            label5 = new Label();
            label6 = new Label();
            label7 = new Label();
            label8 = new Label();
            button1 = new Button();
            button2 = new Button();
            listBox1 = new ListBox();
            listBox2 = new ListBox();
            SuspendLayout();
            //
            // textBox1
            //
            textBox1.Location = new Point(346, 7);
            textBox1.Margin = new Padding(3, 2, 3, 2);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(246, 23);
            textBox1.TabIndex = 0;
            //
            // textBox2
            //
            textBox2.Location = new Point(346, 44);
            textBox2.Margin = new Padding(3, 2, 3, 2);
            textBox2.Name = "textBox2";
            textBox2.Size = new Size(110, 23);
            textBox2.TabIndex = 1;
            //
            // textBox3
            //
            textBox3.Location = new Point(346, 87);
            textBox3.Margin = new Padding(3, 2, 3, 2);
            textBox3.Name = "textBox3";
            textBox3.Size = new Size(197, 23);
            textBox3.TabIndex = 2;
            //
            // dateTimePicker1
            //
            dateTimePicker1.Location = new Point(361, 128);
            dateTimePicker1.Margin = new Padding(3, 2, 3, 2);
            dateTimePicker1.Name = "dateTimePicker1";
            dateTimePicker1.Size = new Size(219, 23);
            dateTimePicker1.TabIndex = 3;
            //
            // label1
            //
            label1.AutoSize = true;
            label1.Location = new Point(265, 7);
            label1.Name = "label1";
            label1.Size = new Size(58, 15);
            label1.TabIndex = 4;
            label1.Text = "Фамилия";
            //
            // label2
            //
            label2.AutoSize = true;
            label2.Location = new Point(265, 46);
            label2.Name = "label2";
            label2.Size = new Size(31, 15);
            label2.TabIndex = 5;
            label2.Text = "Имя";
            //
            // label3
            //
            label3.AutoSize = true;
            label3.Location = new Point(265, 89);
            label3.Name = "label3";
            label3.Size = new Size(58, 15);
            label3.TabIndex = 6;
            label3.Text = "Отчество";
            //
            // label4
            //
            label4.AutoSize = true;
            label4.Location = new Point(265, 128);
            label4.Name = "label4";
            label4.Size = new Size(90, 15);
            label4.TabIndex = 7;
            label4.Text = "Дата рождения";
            //
            // textBox4
            //
            textBox4.Location = new Point(346, 170);
            textBox4.Margin = new Padding(3, 2, 3, 2);
            textBox4.Name = "textBox4";
            textBox4.Size = new Size(300, 23);
            textBox4.TabIndex = 8;
            //
            // textBox5
            //
            textBox5.Location = new Point(346, 214);
            textBox5.Margin = new Padding(3, 2, 3, 2);
            textBox5.Name = "textBox5";
            textBox5.Size = new Size(230, 23);
            textBox5.TabIndex = 9;
            //
            // dateTimePicker2
            //
            dateTimePicker2.Location = new Point(377, 252);
            dateTimePicker2.Margin = new Padding(3, 2, 3, 2);
            dateTimePicker2.Name = "dateTimePicker2";
            dateTimePicker2.Size = new Size(219, 23);
            dateTimePicker2.TabIndex = 10;
            //
            // textBox6
            //
            textBox6.Location = new Point(346, 290);
            textBox6.Margin = new Padding(3, 2, 3, 2);
            textBox6.Name = "textBox6";
            textBox6.Size = new Size(333, 23);
            textBox6.TabIndex = 11;
            //
            // label5
            //
            label5.AutoSize = true;
            label5.Location = new Point(265, 172);
            label5.Name = "label5";
            label5.Size = new Size(40, 15);
            label5.TabIndex = 12;
            label5.Text = "Адрес";
            //
            // label6
            //
            label6.AutoSize = true;
            label6.Location = new Point(265, 214);
            label6.Name = "label6";
            label6.Size = new Size(35, 15);
            label6.TabIndex = 13;
            label6.Text = "ОМС";
            //
            // label7
            //
            label7.AutoSize = true;
            label7.Location = new Point(265, 258);
            label7.Name = "label7";
            label7.Size = new Size(106, 15);
            label7.TabIndex = 14;
            label7.Text = "Дата поступления";
            //
            // label8
            //
            label8.AutoSize = true;
            label8.Location = new Point(265, 295);
            label8.Name = "label8";
            label8.Size = new Size(52, 15);
            label8.TabIndex = 15;
            label8.Text = "Диагноз";
            //
            // button1
            //
            button1.Location = new Point(30, 307);
            button1.Margin = new Padding(3, 2, 3, 2);
            button1.Name = "button1";
            button1.Size = new Size(148, 22);
            button1.TabIndex = 16;
            button1.Text = "Сохранить в файл";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            //
            // button2
            //
            button2.Location = new Point(30, 268);
            button2.Margin = new Padding(3, 2, 3, 2);
            button2.Name = "button2";
            button2.Size = new Size(82, 22);
            button2.TabIndex = 17;
            button2.Text = "Добавить";
            button2.UseVisualStyleBackColor = true;
            button2.Click += button2_Click;
            //
            // listBox1
            //
            listBox1.FormattingEnabled = true;
            listBox1.ItemHeight = 15;
            listBox1.Location = new Point(30, 9);
            listBox1.Margin = new Padding(3, 2, 3, 2);
            listBox1.Name = "listBox1";
            listBox1.Size = new Size(206, 139);
            listBox1.TabIndex = 18;
            listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
            //
            // listBox2
            //
            listBox2.FormattingEnabled = true;
            listBox2.ItemHeight = 15;
            listBox2.Location = new Point(30, 151);
            listBox2.Margin = new Padding(3, 2, 3, 2);
            listBox2.Name = "listBox2";
            listBox2.Size = new Size(206, 109);
            listBox2.TabIndex = 19;
            //
            // Form1
            //
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(700, 338);
            Controls.Add(listBox2);
            Controls.Add(listBox1);
            Controls.Add(button2);
            Controls.Add(button1);
            Controls.Add(label8);
            Controls.Add(label7);
            Controls.Add(label6);
            Controls.Add(label5);
            Controls.Add(textBox6);
            Controls.Add(dateTimePicker2);
            Controls.Add(textBox5);
            Controls.Add(textBox4);
            Controls.Add(label4);
            Controls.Add(label3);
            Controls.Add(label2);
            Controls.Add(label1);
            Controls.Add(dateTimePicker1);
            Controls.Add(textBox3);
            Controls.Add(textBox2);
            Controls.Add(textBox1);
            Margin = new Padding(3, 2, 3, 2);
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private TextBox textBox1;
        private TextBox textBox2;
        private TextBox textBox3;
        private DateTimePicker dateTimePicker1;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private TextBox textBox4;
        private TextBox textBox5;
        private DateTimePicker dateTimePicker2;
        private TextBox textBox6;
        private Label label5;
        private Label label6;
        private Label label7;
        private Label label8;
        private Button button1;
        private Button button2;
        private ListBox listBox1;
        private ListBox listBox2;
    }
}

Код для Patient.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task2
{
    public class Patient
    {
        public string SuName {  get; set; }
        public string Name { get; set; }
        public string FathersName { get; set; }
        public DateTime DataBirth {  get; set; }
        public string Adress { get; set; }
        public string Oms { get; set; }
        public DateTime DataPostuplenia { get; set; }
        public string Diagnoz { get; set; }

    }
}

CC BY-SA 4.0
Новый участник
Anna — новый участник сайта. Будьте снисходительны, задавая уточняющие вопросы, комментируя и отвечая. Почитайте про нормы поведения.
1

0

Ваш ответ