0

Код для Form1.cs


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AnyaTask10
{
    public partial class Form1 : Form
    {
        public List<Sotrudnik> SotrudnikList = new List<Sotrudnik>();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string[] lines = File.ReadAllLines(ofd.FileName);
                string otdel = lines[0];
                for (int i = 1; i < lines.Length; i++)
                {
                    Sotrudnik so = new Sotrudnik();
                    string[] data = lines[i].Split(new string[] { ", " }, StringSplitOptions.None);
                    so.Otdel = otdel;
                    so.Fullname = data[0];
                    so.DataRozd = DateTime.ParseExact(data[1], "dd.MM.yyyy", CultureInfo.InvariantCulture);
                    so.Pol = data[2];
                    so.Obrazovanie = data[3];
                    so.Dolzhnost = data[4];
                    so.SemPolozh = data[5];

                    if (data.Length > 6)
                    {
                        int rebenokLines = 2;
                        int rebenokInfoStartIndex = 7;
                        for (int j = 0; j < int.Parse(data[6]); j++)
                        {
                            Rebenok rebenok = new Rebenok();
                            rebenok.Name = data[rebenokInfoStartIndex + j * rebenokLines];
                            rebenok.DataRozd = DateTime.ParseExact(data[rebenokInfoStartIndex + j * rebenokLines + 1], "dd.MM.yyyy", CultureInfo.InvariantCulture);
                            so.Deti.Add(rebenok);
                        }
                    }
                    SotrudnikList.Add(so);
                }
            }

            listBox1.Items.Clear();
            listBox1.Items.AddRange(SotrudnikList.ToArray());
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string poiskText = textBox1.Text.Trim().ToLower();

            List<Sotrudnik> filtered = new List<Sotrudnik>(SotrudnikList);

            if (poiskText.Length > 0)
            {
                filtered.Clear();

                foreach (Sotrudnik so in SotrudnikList)
                {
                    string sotrText = $"{so.Otdel}{so.Fullname}{so.Obrazovanie}{so.Dolzhnost}{so.Pol}{so.SemPolozh}".ToLower();
                    if (sotrText.Contains(poiskText))
                    {
                        filtered.Add(so);
                        continue;
                    }

                    if (DateTime.TryParse(poiskText, out DateTime data))
                    {
                        foreach (Rebenok r in so.Deti)
                        {
                            if (r.DataRozd > data)
                            {
                                filtered.Add(so);
                                break;
                            }
                        }
                    }
                }
            }

            listBox1.Items.Clear();
            listBox1.Items.AddRange(filtered.ToArray());
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem is Sotrudnik so)
            {
                string info = $"{so.DataRozd}\n{so.Pol}\n{so.Dolzhnost}\n{so.Obrazovanie}\n{so.SemPolozh}\n";
                if (so.Deti.Count > 0)
                {
                    info += $"Дети ({so.Deti.Count}):\n";
                    foreach (Rebenok rebenok in so.Deti)
                    {
                        info += rebenok + "\n";
                    }
                }
                label1.Text = info;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("sotrudniki.txt"))
            {
                BinaryReader br = new BinaryReader(File.Open("sotrudniki.txt", FileMode.Open));
                int count = br.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    Sotrudnik so = new Sotrudnik();
                    so.Otdel = br.ReadString();
                    so.Fullname = br.ReadString();
                    so.DataRozd = DateTime.FromBinary(br.ReadInt64());
                    so.Pol = br.ReadString();
                    so.Dolzhnost = br.ReadString();
                    so.Obrazovanie = br.ReadString();
                    so.SemPolozh = br.ReadString();
                    int detiCount = br.ReadInt32();
                    for (int j = 0; j < detiCount; j++)
                    {
                        Rebenok rebenok = new Rebenok();
                        rebenok.Name = br.ReadString();
                        rebenok.DataRozd = DateTime.FromBinary(br.ReadInt64());
                        so.Deti.Add(rebenok);
                    }
                    SotrudnikList.Add(so);
                }

                listBox1.Items.Clear();
                listBox1.Items.AddRange(SotrudnikList.ToArray());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BinaryWriter bw = new BinaryWriter(File.Open("sotrudniki.txt", FileMode.Create));
            bw.Write(SotrudnikList.Count);
            foreach (Sotrudnik so in SotrudnikList)
            {
                bw.Write(so.Otdel);
                bw.Write(so.Fullname);
                bw.Write(so.DataRozd.ToBinary());
                bw.Write(so.Pol);
                bw.Write(so.Dolzhnost);
                bw.Write(so.Obrazovanie);
                bw.Write(so.SemPolozh);
                bw.Write(so.Deti.Count);
                foreach (Rebenok rebenok in so.Deti)
                {
                    bw.Write(rebenok.Name);
                    bw.Write(rebenok.DataRozd.ToBinary());
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndices.Count > 0)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName))
                    {
                        foreach (int index in listBox1.SelectedIndices)
                        {
                            Sotrudnik so = SotrudnikList[index];
                            string sotrText = $"{so.Fullname}, {so.DataRozd.ToString("dd.MM.yyyy")}, {so.Pol}, {so.Obrazovanie}, {so.Dolzhnost}, {so.SemPolozh}";
                            if (so.Deti.Count > 0)
                            {
                                sotrText += $", {so.Deti.Count}";
                                foreach (Rebenok rebenok in so.Deti)
                                {
                                    sotrText += $", {rebenok.Name}, {rebenok.DataRozd.ToString("dd.MM.yyyy")}";
                                }
                            }
                            sw.WriteLine(sotrText);
                        }
                    }
                }
            }
        }
    }
}

Код для Form1.Designer.cs


namespace AnyaTask10
{
    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.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // listBox1
            //
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 16;
            this.listBox1.Location = new System.Drawing.Point(33, 68);
            this.listBox1.Margin = new System.Windows.Forms.Padding(4);
            this.listBox1.Name = "listBox1";
            this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
            this.listBox1.Size = new System.Drawing.Size(317, 324);
            this.listBox1.TabIndex = 0;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(33, 431);
            this.button1.Margin = new System.Windows.Forms.Padding(4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(159, 28);
            this.button1.TabIndex = 1;
            this.button1.Text = "Загрузить отдел";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(33, 38);
            this.textBox1.Margin = new System.Windows.Forms.Padding(4);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(317, 22);
            this.textBox1.TabIndex = 2;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(375, 68);
            this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(44, 16);
            this.label1.TabIndex = 3;
            this.label1.Text = "label1";
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(199, 431);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(220, 28);
            this.button2.TabIndex = 4;
            this.button2.Text = "Сохранить общий список";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(425, 431);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(220, 28);
            this.button3.TabIndex = 5;
            this.button3.Text = "Сохранить выбранные";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1067, 554);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.listBox1);
            this.Margin = new System.Windows.Forms.Padding(4);
            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.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
    }
}


Код для Rebenok.cs


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

namespace AnyaTask10
{
    [Serializable]
    public class Rebenok
    {
        public string Name { get; set; }
        public DateTime DataRozd { get; set; }

        public override string ToString()
        {
            return $"{Name} {DataRozd}";
        }
    }
}

Код для Sotrudnik.cs


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

namespace AnyaTask10
{
    [Serializable]
    public class Sotrudnik
    {
        public string Otdel { get; set; }
        public string Fullname { get; set; }
        public DateTime DataRozd { get; set; }
        public string Pol { get; set; }
        public string Obrazovanie { get; set; }
        public string Dolzhnost { get; set; }
        public string SemPolozh { get; set; }
        public List<Rebenok> Deti { get; set; } = new List<Rebenok>();

        public override string ToString()
        {
            return Fullname;
        }
    }
}

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

0

Ваш ответ